为了账号安全,请及时绑定邮箱和手机立即绑定

linq to sql 扩展 where in 语句问题

linq to sql 扩展 where in 语句问题

绝地无双 2018-12-06 20:26:01
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 else15 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 语句,但是现在只能传一组条件如何改造可以传多组条件
查看完整描述

2 回答

?
慕勒3428872

TA贡献1848条经验 获得超5个赞

当条件多时,效率很低。

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

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

查看完整回答
反对 回复 2019-01-21
  • 2 回答
  • 0 关注
  • 599 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信