我试过重复不同的事情来做到这一点。 我需要查询数据库并只返回列表中包含日期的记录 - 就像动态的“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 –