然后我比较关注的是用linq替代现有的功能的方案,比如动态条件查询的功能,现在linq的动态查询方法有2种:
1、Anders Hejlsberg 推荐的,也就是在 LINQ samples 里的.
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1752078&SiteID=1
代码:
var query
=
db.Customers.
Where( " City = @0 and Orders.Count >= @1 " , " London " , 10 ).
OrderBy( " CompanyName " ).
Select( " new(CompanyName as Name, Phone) " );
db.Customers.
Where( " City = @0 and Orders.Count >= @1 " , " London " , 10 ).
OrderBy( " CompanyName " ).
Select( " new(CompanyName as Name, Phone) " );
2、Wayward 也有另外的方案,详见
http://blogs.msdn.com/mattwar/archive/2006/05/10/594966.aspx
主要代码:
IQueryable q
=
;
ParameterExpression p = Expression.Parameter( typeof (Customer), " c " );
LambdaExpression predicate =
QueryExpression.Lambda( " c.City = 'London' " , p);
Expression where = QueryExpression.Where(q.Expression, predicate);
q = q.CreateQuery(where);
ParameterExpression p = Expression.Parameter( typeof (Customer), " c " );
LambdaExpression predicate =
QueryExpression.Lambda( " c.City = 'London' " , p);
Expression where = QueryExpression.Where(q.Expression, predicate);
q = q.CreateQuery(where);
我比较喜欢后者。
另外 Rico 针对linq to sql 做了一系列的性能分析,总体结论是linq性能不错,有些地方甚至比ado.net还要好。
DLinq (Linq to SQL) Performance (Part 4)
DLinq (Linq to SQL) Performance (Part 3)
DLinq (Linq to SQL) Performance (Part 2)
DLinq (Linq to SQL) Performance (Part 1)
如 DLinq (Linq to SQL) Performance (Part 4)的结果是:
no linq | with linq | |||||||
select | update | insert | original | compiled | update | compiled | insert | |
run 1 | 915.25 | 4.87 | 4.29 | 497.81 | 858.07 | 20.65 | 21.05 | 20.25 |
run 2 | 916.25 | 5.02 | 4.76 | 491.59 | 864.60 | 20.34 | 20.62 | 12.02 |
run 3 | 942.86 | 4.87 | 4.66 | 496.57 | 859.11 | 21.03 | 20.47 | 16.08 |
average | 924.79 | 4.92 | 4.57 | 495.32 | 860.59 | 20.67 | 20.71 | 16.12 |
The units for all of the above are test iterations per second so bigger is better.
dlinq | nolinq | ratio | ||
select | 495.32 | 924.79 | 53.56% | |
compiled select | 860.59 | 924.79 | 93.06% | |
update | 20.67 | 4.92 | 420.19% | (DLinq is faster) |
compiled update | 20.71 | 4.92 | 421.00% | (DLinq is faster) |
insert | 16.12 | 4.57 | 352.66% | (DLinq is faster) |