2016-09-22 22 views
1

我試過重複不同的事情來做到這一點。 我需要查詢數據庫並只返回列表中包含日期的記錄 - 就像動態的「Where」語句一樣。 我相信它會涉及=>但無法獲得正確的語法。Linq以實體返回包含來自另一個列表中的元素的列表

我在下面建立一個短列表來測試,但它可以有任何數量的項目。

下面只需返回datesToShow中包含total.date_reference的記錄。

  List<DateTime> datesToShow = new List<DateTime>(); 
     datesToShow.Add(new DateTime(2016, 9, 22)); 
     datesToShow.Add(new DateTime(2016, 9, 21)); 

     var todays_totals = (from total in dbContext.daily_totals 
           select new 
          { 
           total.customer.customer_name, 
           total.date_reference, 
           total.EDI_PODs_sent, 
           total.open_stops, 
           total.total_pieces, 
           total.new_stops, 
           total.total_weight, 
           total.Unsent_PODs_released, 
           total.Unsent_PODs_unreleased, 
           total.last_updated 
          }).ToArray(); 

如果我這樣做:

var todays_totals = (from total in dbContext.daily_totals 
          select new 
          { 
           total.customer.customer_name, 
           total.date_reference, 
           total.EDI_PODs_sent, 
           total.open_stops, 
           total.total_pieces, 
           total.new_stops, 
           total.total_weight, 
           total.Unsent_PODs_released, 
           total.Unsent_PODs_unreleased, 
           total.last_updated 
          }).Where(el => datesToShow.Contains(el.date_reference)).ToArray(); 

我得到一個 「未知的方法在哪裏?(?)」 我一直在使用這兩個列表,並像一個數組的嘗試:

DateTime[] datesToShow = new DateTime[] 
     { 
      new DateTime (2016,9,22), 
      new DateTime (2016,9,23) 
     }; 

我也會很滿意一個新的結果集,這是todays_totals的一個子集。 類似下面的(這裏我其實從開始)

var newList = (from t in todays_totals where (t=>datesToShow.Contains(t.date_reference))).ToArray(); 
+0

是你反對使用lambda表達式? –

+0

完全沒有BviLLeKid –

回答

2

您可以嘗試使用Contains擴展方法:

 var todays_totals = (from total in dbContext.daily_totals 
          where datesToShow.Contains(DbFunctions.TruncateTime(total.date_reference)) 
          select new 
         { 
          total.customer.customer_name, 
          total.date_reference, 
          total.EDI_PODs_sent, 
          total.open_stops, 
          total.total_pieces, 
          total.new_stops, 
          total.total_weight, 
          total.Unsent_PODs_released, 
          total.Unsent_PODs_unreleased, 
          total.last_updated 
         }).ToArray(); 

DbFunction.TruncateTime會幫助你清理你的日期在他們與時間的情況下。

+0

不管我如何嘗試並輸入這一個,我得到一個錯誤,如果我保留DbFunctions那麼我得到一個未知的方法在哪裏(?),即使我刪除TruncateTime部分,我也會得到同樣的結果。 –

+0

請發佈錯誤以查看發生了什麼 – octavioccl

+0

還有第二個錯誤:錯誤參數1:無法從'System.DateTime?'轉換到'System.DateTime'\t - 這似乎是指向的方式,但...? –

0

使用Lambda表達式:

List<DateTime> datesToShow = new List<DateTime>(); 

datesToShow.Add(new DateTime(2016, 9, 22)); 
datesToShow.Add(new DateTime(2016, 9, 21)); 

var todays_totals = dbContext.daily_totals.Where(o=> datesToShows.Contains(o.date_reference)).ToList(); 

//Result should be a list that contains records that match those 2 dates. 

希望這有助於!

0

這裏有兩種方法可以做到你想要什麼,是basicly相同:

  1. 使用LINQ where聲明。他們可以返回任何有效的C#表達bool

    (from total in dbContext.daily_totals 
    where datesToShow.Contains(total.date_reference) 
    select new 
    { 
        // your select items... 
    }).ToArray(); 
    
  2. WHERE條款,即意志,正如你所指出,包含lambda表達式使用LINQ擴展方法:

    (from total in dbContext.daily_totals 
    select new 
    { 
        // your select items... 
    }) 
    .Where(el => datesToShow.Contains(el.date_reference)) 
    .ToArray(); 
    
+0

任何時候我用「where datesToShow.Contains(total.date_reference)」我得到一個未知的成員'date_reference'錯誤。 –

相關問題