簡體   English   中英

Linq到實體以返回包含另一個列表中的元素的列表

[英]Linq to entities to return a list that contains elements from another list

我已經嘗試過重復不同的事情來做到這一點。 我需要查詢數據庫並僅返回具有包含在列表中的日期的記錄-像動態的“ Where”語句。 我確定它將涉及=>,但無法獲得正確的語法。

我在下面建立一個簡短列表進行測試,但是可以包含任意數量的項目。

以下僅需要返回傳回dateToShow中的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();

我得到了“未知方法Where(?)...”,我嘗試同時使用列表和數組:

   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();

您可以嘗試使用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將幫助您清理日期(如果有時間的話)。

使用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.

希望這可以幫助!

有兩種方法可以做您想要的事情,基本上是相同的:

  1. 使用LINQ where語句。 他們可以采用任何返回bool有效C#表達式:

     (from total in dbContext.daily_totals where datesToShow.Contains(total.date_reference) select new { // your select items... }).ToArray(); 
  2. WHERE子句使用LINQ擴展方法,正如您所指出的,它將包含一個lambda表達式:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM