Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用LINQ筛选列表_Linq - Fatal编程技术网

使用LINQ筛选列表

使用LINQ筛选列表,linq,Linq,您好,我有一个名为EmpowerCalendarCode的类: 它具有以下字段CalendarCode、CalendarName、EffectiveStartDate、EmpowerTaxType、LongAgencyCode、TaxDepositFrequencyBaseTypeID 我想根据LongAgencyCode和EmpowerTaxType筛选此列表。让我们说: public IList<EmpowerCalendarCode> GetEmpowerCalendarCod

您好,我有一个名为EmpowerCalendarCode的类:

它具有以下字段CalendarCode、CalendarName、EffectiveStartDate、EmpowerTaxType、LongAgencyCode、TaxDepositFrequencyBaseTypeID

我想根据LongAgencyCode和EmpowerTaxType筛选此列表。让我们说:

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