首页 新闻 会员 周边

linq to sql 扩展 where in 语句问题

0
[已解决问题] 解决于 2010-05-22 09:22
1 public static IQueryable<TSource> WhereIn<TSource, TKey>(this IQueryable<TSource> source1, Expression<Func<TSource, TKey>> keySelector, IEnumerable<TKey> source2)
2 {
3 if (null == source1)
4 throw new ArgumentNullException("source1");
5 if (null == keySelector)
6 throw new ArgumentNullException("keySelector");
7 if (null == source2)
8 throw new ArgumentNullException("source2"); Expression where = null;
9 foreach (TKey value in source2)
10 {
11 Expression equal = Expression.Equal(keySelector.Body, Expression.Constant(value, typeof(TKey)));
12 if (null == where)
13 where = equal;
14 else
15 where = Expression.OrElse(where, equal);
16 }
17 return source1.Where<TSource>(Expression.Lambda<Func<TSource, bool>>(where, keySelector.Parameters));
18 }

上面这段是linq to sql 扩展 where in 语句,但是现在只能传一组条件如何改造可以传多组条件

Drin Chan的主页 Drin Chan | 初学一级 | 园豆:6
提问于:2010-05-10 10:05
< >
分享
最佳答案
0

希望我的这篇博客能对你有用。

http://www.cnblogs.com/MyNameEPC/archive/2009/07/05/1517212.html

周巍 | 小虾三级 |园豆:735 | 2010-05-10 14:07
其他回答(1)
0

当条件多时,效率很低。

它转换成了 (xx=?)or (xx=?)or (xx=?)or (xx=?)... 这种形式

能不能转换成 where in (?,?,...) 这种形式 

Lexy | 园豆:202 (菜鸟二级) | 2012-09-27 13:59
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册