我試過重複不同的事情來做到這一點。 我需要查詢數據庫並只返回列表中包含日期的記錄 - 就像動態的「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();
是你反對使用lambda表達式? –
完全沒有BviLLeKid –