首页 新闻 会员 周边

利用foreach如何实现linq 之 where 多条件查询(where叠加)???

-1
悬赏园豆:10 [已关闭问题] 关闭于 2011-10-29 11:24

利用foreach如何实现linq 之 where 多条件查询???


//这里不能实现叠加,所以提交的条件是最后一个
//应如何实现多个where语句的叠加???最后一次性提交所有条件进行查询

View Code
  class A

public DbContext context= new e6waDBEntities();;

public static T ReadByExpression(List<Expression<Func<T, bool>>> t)
{
IQueryable<T> ab = null;
foreach (Expression<Func<T, bool>> l in t)
{
ab = context.Set<T>().Where(l);
//这里不能实现叠加,所以提交的条件是最后一个
//应如何实现多个where语句的叠加???最后一次性提交所有条件进行查询
}
return ab.FirstOrDefault<T>();
}

 

**************************调用************************
e6wa.Entity.User userInfo = new User();
List<Expression<Func<User, bool>>> t = new List<Expression<Func<User, bool>>>();
t.Add(u => u.UserEmail == userName);
t.Add(u => u.UserPassword == userPassword);

 

            #region 最基本的实现方法
using (e6waDBEntities db = new e6waDBEntities())
{
var tu = db.Users.Where(u => u.UserEmail == userName).Where(p => p.UserPassword == userPassword);
if (tu.Count() >= 1)
{ return true; }
else
{ return false; }
}
#endregion
伊牛娃的主页 伊牛娃 | 菜鸟二级 | 园豆:207
提问于:2011-10-05 15:46
< >
分享
所有回答(2)
0

for循环里面,应该是ab 来进行过滤,类似下面的。

IQueryable<T>  ab = context.Set<T>();

foreach (Expression<Func<T, bool>> l in t)        

{                

     ab = ab.Where(l);

}

心火 | 园豆:375 (菜鸟二级) | 2011-10-05 18:56
0

mark

vs_bug | 园豆:205 (菜鸟二级) | 2011-11-16 16:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册