views:

491

answers:

1

Hi,

I have a table like below:

EmployeeId
EmployeeName

RequestId
RequestName

EmployeeId
RequestId

I need to a to assign requests in a sequential fashion(those who has mininum number of requests).

Can I know how to get employee who has minimum requests using linq???

Thanks, Mahesh

+2  A: 

Assuming the class containing EmployeeID and RequestID (the third table) is named "Foo", it could be

(from f in db.Foos
 group by f.EmployeeID into g
 orderby g.Count()
 select new { f.EmployeeID, g.Count() }).First()

This is drycoded and may be wrong. =)

Jens
Great answer for this very bad question.
Steven
Thanks for the reply. will this change if I am using LINQ to SQL???
Mahesh
@Mahesh: This is written for LINQ to SQL.
Jens
Sorry guys for posting without any clarity. I just need to convert the following sql statement to LINQ. SELECT EmployeeId,MIN(C) FROM ( SELECT EmployeeId,COUNT(*) AS C FROM Employee emp INNER JOIN EmployeeRequsts empreq ON empreq.EmployeeId=emp.EmployeeId GROUP BY empreq.EmployeeId )
Mahesh
@Mahesh: This should be added to the question. Maybe my SQL is rusty, but does this query really do what you describe? Try my LINQ query and check whether this is the data you expected.
Jens