Linq to sql 实现多条件的动态查询(方法一)

多条件动态查询在开发中太常见了,使用sql语句拼接很容易实现,但是在使用linq 的时候我却觉得很麻烦,思来想去,其实不然,linq 实现多条件动态查询一样可以变的很简单。话不多说,直接上例子,供大家参考。

      根据5个动态条件查询:

[csharp]  view plain  copy
 print ?
  1. /// <summary>  
  2. /// Linq to sql 多字段动态查询  
  3.  /// </summary>  
  4. /// <returns></returns>  
  5. private List<TVacant> ViewBinding(ModelDataContext db,string fyno,string brd,string area,string city,string pos)  
  6. {  
  7.     Expression<Func<TVacant, bool>> expr = n => GetCondition(n,fyno,brd,area,city,pos);  
  8.     var xQuery = db.TVacant.Where<TVacant>(expr.Compile());  
  9.     return xQuery.ToList<TVacant>();  
  10. }  
  11. private bool GetCondition(TVacant tb,string fyno,string brd,string area,string city,string pos)  
  12. {  
  13.     bool boolResult = true;  
  14.     if (!String.IsNullOrEmpty(fyno))  
  15.     {  
  16.         boolResult &= tb.fy_no == fyno;  
  17.     }  
  18.     if (!String.IsNullOrEmpty(brd))  
  19.     {  
  20.         boolResult &= tb.brd_no == brd;  
  21.     }  
  22.     if (!String.IsNullOrEmpty(area))  
  23.     {  
  24.         boolResult &= tb.area_no == area;  
  25.     }  
  26.     if (!String.IsNullOrEmpty(city))  
  27.     {  
  28.         boolResult &= tb.city_no == city;  
  29.     }  
  30.     if (!String.IsNullOrEmpty(pos))  
  31.     {  
  32.         boolResult &= tb.pos_no == pos;  
  33.     }  
  34.     return boolResult;  
  35. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值