开发者

Dynamic where in LINQ

Hi I have this LINQ query:

var q =
    (from vr in Util.db.ValuationsRequests
     where vr.dtSubmitted != null
     select vr
     ).AsEnumerable<ValuationsRequest>();

But I want to do a search开发者_高级运维 with 3 more parameters: paramValuationId (int), paramValue (boolean), paramTitle (string).

Something like:

if (paramTitle != string.empty)
//add this field to the where

But if the paramTitle is empty I do not want to search for it.

What is the correct way of doing this?


string paramTitle = "hello";
var q =
    (from vr in Util.db.ValuationsRequests
     where vr.dtSubmitted != null 
       && ( paramTitle == "" || vr.paramTitle == paramTitle)
     select vr
     ).AsEnumerable<ValuationsRequest>();


var q =
(from vr in Util.db.ValuationsRequests
 where vr.dtSubmitted != null
 select vr
 ).AsEnumerable<ValuationsRequest>();

if(!string.IsNullOrEmpty(paramTitle))
   q = q.Where(p => p.ParamTitle == paramTitle);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜