我有一个表(我正在使用实体模型)并使用LINQ查询过滤该表,该查询工作正常。 现在我想根据日期数组过滤记录。我无法在日期数组上实现IN子句
filteredList = leadsNewAndScheduled.Where(lead =>
(LeadTypeIDs.Contains(lead.TYPE_ID.ToString()) ||
LeadTypeIDs == string.Empty) &&
(priorityIDs.Contains(lead.PRIORITY_ID.ToString()) ||
priorityIDs == string.Empty) &&
(((lead.EXPIRED_ON <= dateExpiry ||
Convert.ToDateTime(lead.EXPIRED_ON) == DateTime.Today.Date) &&
lead.STATUS_ID == (int)Enumerations.LeadStatus.New) ||
lead.STATUS_ID == (int)Enumerations.LeadStatus.Active) &&
(lead.START_TIME IN (arrAppointmentDates))
).ToList();
我希望你的帮助如下
(lead.START_TIME IN (arrAppointmentDates))
提前致谢。
答案 0 :(得分:2)
编写没有日期条件的查询,如
var query = leadsNewAndScheduled.Where(lead =>
(LeadTypeIDs.Contains(lead.TYPE_ID.ToString()) ||
LeadTypeIDs == string.Empty) && ....
然后写
var predicate = PredicateBuilder.False<Lead>();
foreach (DateTime date in dates)
{
DateTime temp = date;
predicate = predicate.Or (p => p.START_TIME == temp);
}
var result = query.Where(predicate).ToList(); // Don't call ToList() earlier
但是请注意,如果您正在使用Entity Framework,则需要在实体集上调用AsExpandable(),然后才能在其上应用谓词:
return objectContext.Products.AsExpandable().Where (predicate);
答案 1 :(得分:1)
我在声明日期列表然后在LINQ查询中应用contains子句时解决了这个问题。
示例:
//list of dates.
List<DateTime> arrAppointmentDates;
//change in query
arrAppointmentDates.Contains(Convert.ToDateTime(lead.START_TIME).Date)