how to select distinct values in a column in entity framework?

asp.net-mvc c# entity-framework

Question

My DB Table contains ContractorName as one of the columns. There are some other columns like workinghoursforjoc, manpowerjoc, workinghoursforcontractor, manpowercontractor. But, I want to retrieve the unique names of the contractor.

I tried in the following way.

var contractors = context.ManHoursWorked.Where(j => j.JOCID == jocID).Select(c => c.ContractorName).Distinct();

But I am getting the error

Entity Command Execution Exception

1
2
1/15/2016 4:06:16 PM

Accepted Answer

Add ToList() call at the end of the expression to materialize the query:

var contractors = context.ManHoursWorked.Where(j => j.JOCID == jocID).Select(c => c.ContractorName).Distinct().ToList();
3
1/15/2016 4:30:42 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow