用于选择具有第一等级的记录的linq查询

时间:2013-12-17 08:00:14

标签: c# sql linq

我有一个表格,其中我有多个行具有相同的PayFrequencyId

我的sql查询对我有用OK好了如下

SELECT * 
FROM   (SELECT payfrequencyid, 
               customname, 
               payfromdate, 
               paytodate, 
               checkdate, 
               Rank() 
                 OVER( 
                   partition BY payfrequencyid 
                   ORDER BY payfromdate) AS 'RowRank' 
        FROM   payfrequencycalendar 
        WHERE  ispaid = 0 
               AND companyid = 3) AS T 
WHERE  rowrank = 1 

但我必须在linq中使用它。

我尝试过类似的东西,但它不适合我。

IList<IPayFrequencyCalendar> objPayFrequencyInfo = 
    objPayFrequencyManager.GetAll()
        .Where(x => 
            x.CompanyId == SessionWrapper.CompanyId 
            && x.IsPaid == false)
        .GroupBy(o => o.PayFrequencyId)
        .OrderBy(x => x.PayFromDate).Take(5).ToList();

1 个答案:

答案 0 :(得分:1)

试试这个:

IList<IPayFrequencyCalendar> objPayFrequencyInfo =
     objPayFrequencyManager.GetAll()
                           .Where(x => x.CompanyId == SessionWrapper.CompanyId 
                                     && x.IsPaid == false)
                           .GroupBy(o => o.PayFrequencyId)
                           .Select(g => g.OrderBy(t => t.PayFromDate).First())

                           .ToList();