2009-10-28 65 views
2

我很难解决这个问题,需要在C#,asp.net中创建动态LINQ查询的代码。我有5个下拉列表,它在同一个数据库表中搜索不同的列,并将项目过滤值返回给一个列表框。问题是在DDL中没有选择哪个或全部或哪些将被选择的顺序,但组合的过滤结果应该显示在列表框中。我有一个正在运行的查询,它正在为每个DDL选择单独搜索并返回一列中的结果。必须使用AND添加where子句以将其他DDL选择动态添加到此查询中。由于LinQ查询 - 动态添加地址


public ListItemCollection searchProject(ListItemCollection projList, String searchstr, String columnName) 
{ 
    DataSet DSToReturn = new DataSet(); 

    ListItemCollection returnItems = new ListItemCollection(); 
    DataTable results = (from d in ((DataSet)_MyDataset).Tables["Records"].AsEnumerable() 
         orderby d.Field<string>("Name") ascending 
         where (d.Field<string>(columnName) != null) 
         where d[columnName].ToString().ToLower().Contains(searchstr.ToLower()) 
         select d).CopyToDataTable(); 

    foreach (ListItem li in projList) 
    { 
     if ((from System.Data.DataRow row in results.Rows 
      where li.Value.Equals(row["value"].ToString(), StringComparison.InvariantCultureIgnoreCase) 
      select row["value"]).Count() > 0) 
     returnItems.Add(li); 
    } 

    return returnItems; 
} 
+0

duplicate http://stackoverflow.com/questions/848415/linq-dynamic-where-clause – 2009-10-28 14:52:18

+0

如何使此查询动态添加每个DDL选择的位置,请帮助我使用此代码。 (“Name”) 其中(d.Field (columnName)!= null)数据表结果=(来自((DataSet)_MyDataset中的d)。表格[“记录”] AsEnumerable() orderby d.Field ) where d [columnName] .ToString()。ToLower()。Contains(searchstr.ToLower()) select d).CopyToDataTable(); – menon 2009-10-28 17:07:45

+0

[Linq2SQL“or/and”operators(ANDed/ORed conditions)]的可能重复(http://stackoverflow.com/questions/1450983/linq2sql-or-and-operators-anded-ored-conditions) – 2011-08-03 11:18:26

回答

7

下面是我们如何做到这一点一些示例代码...

private void DataPortal_Fetch(GoalCriteria criteria) 
    { 
     using (var ctx = ContextManager<Data.ExodusDataContext> 
        .GetManager(Database.ApplicationConnection, false)) 
     { 
      this.RaiseListChangedEvents = false; 
      this.IsReadOnly = false; 

      // set option to eager load child object(s) 
      var opts = new System.Data.Linq.DataLoadOptions(); 
      opts.LoadWith<Data.Goal>(row => row.Contact); 
      opts.LoadWith<Data.Goal>(row => row.Sales); 
      opts.LoadWith<Data.Goal>(row => row.Customer); 
      ctx.DataContext.LoadOptions = opts; 

      IQueryable<Data.Goal> query = ctx.DataContext.Goals; 

      if (criteria.Name != null) // Name 
       query = query.Where(row => row.Name.Contains(criteria.Name)); 

      if (criteria.SalesId != null) // SalesId 
       query = query.Where(row => row.SalesId == criteria.SalesId); 

      if (criteria.Status != null) // Status 
       query = query.Where(row => row.Status == (int)criteria.Status); 

      if (criteria.Statuses.Count != 0) // Statuses 
       query = query.Where(row => criteria.Statuses.Contains((GoalStatus)row.Status)); 

      if (criteria.ContactId != null) // ContactId 
       query = query.Where(row => row.ContactId == criteria.ContactId); 

      if (criteria.CustomerId != null) // CustomerId 
       query = query.Where(row => row.CustomerId == criteria.CustomerId); 

      if (criteria.ScheduledDate.DateFrom != DateTime.MinValue) // ScheduledDate 
       query = query.Where(t => t.ScheduledDate >= criteria.ScheduledDate.DateFrom); 
      if (criteria.ScheduledDate.DateTo != DateTime.MaxValue) 
       query = query.Where(t => t.ScheduledDate <= criteria.ScheduledDate.DateTo); 

      if (criteria.CompletedDate.DateFrom != DateTime.MinValue) // ComplatedDate 
       query = query.Where(t => t.CompletedDate >= criteria.CompletedDate.DateFrom); 
      if (criteria.CompletedDate.DateTo != DateTime.MaxValue) 
       query = query.Where(t => t.CompletedDate <= criteria.CompletedDate.DateTo); 

      if (criteria.MaximumRecords != null) // MaximumRecords 
       query = query.Take(criteria.MaximumRecords.Value); 

      var data = query.Select(row => GoalInfo.FetchGoalInfo(row)); 

      this.AddRange(data); 

      this.IsReadOnly = true; 
      this.RaiseListChangedEvents = true; 
     } 
    } 

我们只是检查分配给我们的标准空值的对象,如果它不为空,然后我们追加它来查询。

+0

我绝对新的,请大家帮忙,我将整个Dataportal_Fetch函数复制到Class文件中,并在每行中都出现语法错误。我错过了什么?以及如何在后面的代码中调用此函数? – menon 2009-10-28 16:43:36

+0

在我的情况下,我有_MyDataset正在搜索linq查询,我如何使用,而不是 使用(var ctx = ContextManager .GetManager(Database.ApplicationConnection,false)) – menon 2009-10-28 16:45:19

+0

这里是我的查询:我如何将它添加到你的代码? (“Name”) 其中(d.Field (columnName)!= null)数据表结果=(来自((DataSet)_MyDataset中的d)。表格[“记录”] AsEnumerable() orderby d.Field ) where d [columnName] .ToString()。ToLower()。Contains(searchstr.ToLower()) select d).CopyToDataTable(); – menon 2009-10-28 17:04:07