-1

There are 2 tables: Rents and People

I'm new in linq statements programming. Here is my sql query which i want to transform into linq:

select top 20 r.partnerid, max(p.PartnerName )
from Rents r join People p on p.Partnerid = r.Partnerid
where r.partnerid > 0 
group by  r.partnerid
order by  count(r.rentid) desc

I tried something like this, but I do not know how to get the People.PartnerName into the select statement:

var linqQuery = from r in meta.Rents 
             join p in meta.People on r.PartnerId equals p.PartnerId
             where r.PartnerId > 0 
             group r by r.PartnerId into pp
             select new
             {
                 PartnerId = pp.Key,
                 PartnerName = ??? //  PartnerName is a field/column from People
             } 
             // order by ?  
Florin M.
  • 2,159
  • 4
  • 39
  • 97

2 Answers2

0

You might want to get the first one from the grouped list

PartnerName = pp.First().PartnerName

Or, you could also use max like your original query

PartnerName = pp.Max(x => x.PartnerName)
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

You can do something like

PartnerName = pp.Select(p => p.PartnerName).FirstOrDefault()

You will get some further explanation and a running fiddle from here: Group by in LINQ

Cedric Mendelin
  • 172
  • 1
  • 2
  • 9