Okay, I'll do it. I was planning to post an advertisement, but then I thought, if I post a purely advertising post, I'm sure there will be a lot of curses and bricks and eggs flying everywhere. I might as well put the DAC in. Regarding custom LINQ Let me explain the code that supports expressions. Maybe it will be helpful to some people who write their own data layer or want to write some practical LINQ extensions. Even if it is not helpful, it will serve as a "suggestion", wouldn't it be great!
In previous versions of DAC, the following expression was used to query:
IEnumerable<Issue> q = this.dc.Query<Issue>(Issue._.IssueID >= 0);
This "cute" "_" seems so "immature", especially in this era of rampant LINQ, it seems very "advancing with the times". So there is v2.6 version that supports DAC extension LINQ.
var q = this.dc.Query<Issue>(s => s.IssueID > 0);
or
var q = from s in (this.dc.GetQueryableData<Issue>())
where s.IssueID > 0
select s;
The implementation of the above two methods mainly relies on parsing expressions and assembling SQL statements. Lao Zhao's post is very well written, and you can also look at the source code of DAC (I am inserting advertisements).
What I mainly want to say today is, if we originally have our own expression tree and its parsing function, can we easily "upgrade" it to support LINQ? ! Yes, it's easy!
var q = from s in SystemUser._
where s.FullName == s.FullName.Max
orderby s.UserID
select s.Except(s.Password);
To allow an object to be "where", "orderby", "select", then let it implement methods named "Where", "OrderBy", "Select", or use the Extension Method method, just like in DAC:
public static class QueryExpressionExtension
{
public static RaisingStudio.Data.Expressions.IQueryExpression<T> Where<T>(
this RaisingStudio.Data.Expressions.IQueryExpression<T> source,
Expression<Func<T, RaisingStudio.Data.Expressions.ConditionExpression>> predicate)
{
return (new RaisingStudio.Data.Expressions.QueryExpression<T>(
source.Value,
source.Table,
(predicate.Compile())(source.Value),
source.Columns));
}
public static RaisingStudio.Data.Expressions.IQueryExpression<T> OrderBy<T>(
this RaisingStudio.Data.Expressions.IQueryExpression<T> source,
Expression<Func<T, RaisingStudio.Data.Expressions.ColumnExpression>> predicate)
{ … }
public static RaisingStudio.Data.Expressions.IQueryExpression<T> Select<T>(
this RaisingStudio.Data.Expressions.IQueryExpression<T> source,
Expression<Func<T, object>> predicate)
{ ... }
}
In the implementation of the method, call the Compile method on the incoming "system" expression to return the "custom" expression, and then save these "custom" expressions in the "QueryExpression" )", when the "query" is executed, the saved "custom" expression can be directly taken out. In this way, the query can be implemented by using the original parsing of the "custom" expression in the above code! ! !