tags:

views:

39

answers:

2

Table 1: Lookups

LookUpID
LookUpName
Desc
DisplayOrder

Table 2: BillingRates

BillingRateID
BillingRate
ClientID
LookupID

I want the lookup name to be displayed (sort by Bill rate)

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

It gave me all the row, so I used Distinct(); The lookup Name is still not based on billing rate.

I am new to LINQ. Any pointers would be appreciated.

A: 

Why not just do the OrderBy at the end?

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

EDIT: I am kind of confused but try the following and let me know if that helps.

Kelsey
This is displaying multiple lookup names and multiple BillingRate for example architect 0.75 architect 1.00 architect 1750.00 I only wanted the one look up name based on maximum value. In the above mentioned case it is 1750.00
Kalls
billingRates.BillingRate is decimal. It gives me all the rows for example the lookupName Architect is displayed for each billing rate. I need only one row with maximum billing rate.
Kalls
Wow that is some complex Linq Code and I am learning from you. The above code works in LinqPad. I am sure it works fine when I integrate that in to my project.
Kalls
A: 

First of all, if you have a foreign key relationship set up, LINQ will create the join for you automatically, so it would be just:

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

Otherwise, (with the explicit join)

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();
James Curran
Cannot implicitly convert type AnonymousType #1 to System.Linq.IQueryable Error. This code is called from C# method that returns IQueryable
Kalls
Sorry That I am not clear.here is sample data returned Architect 0.0Architect 1.0Architect 1750.00PM 1.0PM 2.0PM 3500.00I want resultsPM 3500.00Architect 1750.00 First only gave me one row.
Kalls