Linq查询显示正确的排序顺序

时间:2010-09-09 20:22:51

标签: c# linq-to-sql

表1:查找

LookUpID
LookUpName
Desc
DisplayOrder

表2:BillingRates

BillingRateID
BillingRate
ClientID
LookupID

我希望显示查找名称(按比率评分)

DataContext DataContext1 = new DataContext1(AppSettings.ConnectionString);

return ( from Lookups in DataContext1.Lookups
         join BillingRates in DataContext1.BillingRates
         on  Lookups.LookupID equals BillingRates.LookupID
         orderby BillingRates.BillingRate
         select new
         {
             Lookups.LookupID,
             Lookups.LookupName,
             Lookups.Desc
         }).Distinct();

它给了我所有的行,所以我用了Distinct();查找名称仍然不基于结算率。

我是LINQ的新手。任何指针都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

为什么不在最后做OrderBy

return (from Lookups in DataContext1.Lookups 
     join BillingRates in DataContext1.BillingRates 
     on  Lookups.LookupID equals BillingRates.LookupID 
     select new 
     { 
         Lookups.LookupID, 
         Lookups.LookupName, 
         Lookups.Desc,
         BillingRates.BillingRate
     })
     .GroupBy(x => x.LookupID)
     .Select(y => y.OrderByDescending(x => x.BillingRate).First())
     .OrderByDescending(x => x.BillingRate);

编辑:我有点困惑,但请尝试以下操作,如果有帮助请告诉我。

答案 1 :(得分:0)

首先,如果您设置了外键关系,LINQ将自动为您创建连接,所以它只是:

 DataContext1.Lookups.Max(LkUp => LkUp.BillingRate.BillingRate)

否则,(使用显式连接)

return ( from Lookups in DataContext1.Lookups   
     join BillingRates in DataContext1.BillingRates   
     on  Lookups.LookupID equals BillingRates.LookupID 
     orderby BillingRates.BillingRate desc
     select new   
     {   
         Lookups.LookupID,   
         Lookups.LookupName,   
         Lookups.Desc,  
         BillingRates.BillingRate  
     }).First();