2009-10-28 62 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