我有一種情況,可能需要按訂單號或名稱進行搜索。我知道我可以在我的LINQ查詢中添加Where
表達式,但我只想將它添加到我正在搜索的屬性中!我將不知道,直到方法被調用哪個參數將被提供,所以我怎麼能添加適當的條件?如果我不確定哪些屬性需要匹配,如何將Where表達式添加到LINQ查詢中?
public JsonResult Search(int orderNo=0, string firstName="", string lastName="")
{
if (orderNo >0){
//add Condition
}
if (firstName.Length > 0){
//add Condition
}
if (lastName.Length > 0){
//add Condition
}
//get Result
var result = Repository.Orders.Where(???).OrderByDescending(e=> e.orderNo);
//return
}
試過[表達式樹(http://msdn.microsoft.com /en-us/library/bb882637.aspx)? – 2012-04-19 21:33:21