根据特定标准构建linq查询c#

时间:2011-07-05 23:55:34

标签: c# linq list search

我有一个List ListPeople“名为ListPeople的人员列表”和一个对象的类People:

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(所有可能的组合)查询数量?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;
}

祝你好运!

答案 2 :(得分:0)