使用LINQ筛选列表
您好,我有一个名为EmpowerCalendarCode的类: 它具有以下字段CalendarCode、CalendarName、EffectiveStartDate、EmpowerTaxType、LongAgencyCode、TaxDepositFrequencyBaseTypeID 我想根据LongAgencyCode和EmpowerTaxType筛选此列表。让我们说:使用LINQ筛选列表,linq,Linq,您好,我有一个名为EmpowerCalendarCode的类: 它具有以下字段CalendarCode、CalendarName、EffectiveStartDate、EmpowerTaxType、LongAgencyCode、TaxDepositFrequencyBaseTypeID 我想根据LongAgencyCode和EmpowerTaxType筛选此列表。让我们说: public IList<EmpowerCalendarCode> GetEmpowerCalendarCod
public IList<EmpowerCalendarCode> GetEmpowerCalendarCodes(string longAgencyCode, string EmpowerTaxType)
{
//todo:
}
public IList GetEmpowerCalendarCodes(字符串longAgencyCode,字符串EmpowerTaxType)
{
//待办事项:
}
如果我为同一个longAgencyCode获得多个EmpowerCalendarCode,EmpowerTaxType具有相同的TaxDepositFrequencyBaseTypeID,那么我应该选择具有最新生效开始日期的一个。我们应该如何编写LINQ查询。即使我们要做两三次查询才能得到结果,我也没问题
任何想法和建议都将不胜感激 试试这个:
ctx.EmpowerCalendarCodes
.Where(a=>a.LongAgencyCode == longAgencyCode && a.EmpowerTaxType == EmpowerTaxType)
.OrderByDescending(a=>a.EffectiveStartDate)
.FirstOrDefault();
这最多只能返回一个结果。
return yourSource.Where(x => x.LongAgencyCode == longAgencyCode
&& x.EmpowerTaxType == empowerTaxType)
.GroupBy(x => x.TaxDepositFrequencyBaseTypeID,
(k, g) => g.OrderByDescending(x => x.EffectiveStartDate)
.First())
.ToList();