2011-07-05 91 views
0

我有一個List ListPeople「的命名ListPeople人名單」和類對象的人是:構建基於特定的標準LINQ查詢C#

class People 
{ 
    public string Name {get;set;} 
    public DateTime Dob {get;set;} 
    public int Wieght {get;set;} 
} 

我怎麼能與選擇的標準執行搜索用戶:是這樣的: enter image description here

例如,如果用戶會選擇這樣的: enter image description here

然後,我會知道如何設置一個查詢:

var query = (from a in ListPeople 
      where a.Name == "Tom" && 
      a.Weight > 25 && 
      a.Dob < "dateTime.Now() - 7 months" // you know what I mean 
      select a).ToList(); 

我是否必須構建4 * 4 * 4(所有可能組合)查詢數?

+0

Check out Dynamic Linq http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx –

回答

4

您不需要提前構建所有可能的組合,只需要能夠繼續構建查詢。草稿:

var myQuery = ListPeople.AsEnumerable(); 

if (name.Selection == "Is") 
    myQuery = myQuery.Where(p => p.Name == nameInput.Text); 
else if (name.Selection == /* continues */ 

您可以繼續這樣做,爲每個UI元素構建您的查詢謂詞合適的,然後就大功告成後,評價它爲正常。

您可以對Linq-to-SQL或EF做同樣的事情,您只需要使用AsQueryable()而不是AsEnumerable(),這樣您就可以在發送數據庫之前完成查詢。

var myQuery = context.People.AsQueryable(); 
// continues 
+0

@ Tono,我的答案確實有一個錯字,一定要查看更新後的版本。 –

1

爲了做到這一點與LINQ,你需要拉出所有的數據,然後寫入單獨的where子句來過濾掉你需要的東西。您可以將所有變量作爲字符串傳遞給該函數,以便您可以輕鬆地分辨哪些是空的。這是你可以設置的功能:

public List<ListPeople> GetPeopleList(string Name, string opName, string Weight, string opWeight, string DOB, string opDOB) 
{ 
    var items = from a in ListPeople 
       select a; 

    //--- repeat the section below for Weight and DOB 
    if (!string.IsNullOrEmpty(Name)) 
    { 
      switch(opName.ToLower()) 
      { 
       case "contains": 
       { 
        items = items.Where(a => SqlMethods.Like(a.Name, "%" + Name + "%")); 
        break;  
       } 
       case "does not contain": 
       { 
        items = items.Where(a => !SqlMethods.Like(a.Name, "%" + Name + "%")); 
        break;  
       } 
       case "is": 
       { 
        items = items.Where(a => a.Name == Name)); 
        break;  
       } 
       case "is not": 
       { 
        items = items.Where(a => a.Name != Name)); 
        break;  
       } 
      } 
    } 
    //--- end repeat 

    return items.ToList(); 
} 

祝你好運!

編輯: 由於我在這裏的答案,我找到了更好的方法來做這些類型的查詢,它會大大提高性能。結帳http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx 該類允許您以字符串格式動態構建LINQ查詢,然後將其傳遞給查詢。下面是我如何在對房地產搜索功能的房地產網站上使用它的一個示例(精簡下來,便於):

public IQueryable GetSearchResults(string PriceFrom, string PriceTo, string Beds, string Baths, string SqftFrom, string SqftTo, string YearFrom, string YearTo) 
{ 
    DatabaseDataContext db = new DatabaseDataContext(); 

    string WhereClause = string.Empty; 

    if (!string.IsNullOrEmpty(PriceFrom)) 
     WhereClause = "ListPrice >= " + PriceFrom + " AND "; 

    if (!string.IsNullOrEmpty(PriceTo)) 
     WhereClause += "ListPrice <= " + PriceTo + " AND "; 

    if (!string.IsNullOrEmpty(Beds)) 
     WhereClause += "Beds >= " + Beds + " AND "; 

    if (!string.IsNullOrEmpty(Baths)) 
     WhereClause += "FullBaths >= " + Baths + " AND "; 

    if (!string.IsNullOrEmpty(SqftFrom)) 
     WhereClause += "SqFtHeated >= " + SqftFrom + " AND "; 

    if (!string.IsNullOrEmpty(SqftTo)) 
     WhereClause += "SqFtHeated <= " + SqftTo + " AND "; 

    if (!string.IsNullOrEmpty(YearFrom)) 
     WhereClause += "YearBuilt >= " + YearFrom + " AND "; 

    if (!string.IsNullOrEmpty(YearTo)) 
     WhereClause += "YearBuilt <= " + YearTo + " AND "; 

    if (WhereClause.EndsWith(" AND ")) 
     WhereClause = WhereClause.Remove(WhereClause.Length - 5); 

    IQueryable query = db.Listings 
       .Where(WhereClause) 
       .OrderBy("ListPrice descending"); 

    return query; 
} 

祝您好運!