Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting rows with distinct column values using LINQ

I have a generic list of SharePoint list items (List employees)

The SharePoint list contains the two columns, "Employee Name" and "Employee Designation"

I want to get an object List which contains distinct "Employee Designation" column values.

How do I do this using LINQ?

This didnt work for me:

 var designations = from SPListItem employee in employees
                    select new {Designation = employee["Employee Designation"].ToString().Distinct()};
like image 378
ashwnacharya Avatar asked Jan 22 '23 11:01

ashwnacharya


1 Answers

If you are only retrieving the string value of that column, you can avoid writing an equality comparer by selecting a string (instead of an object containing a string property).

var designations = (from SPListItem employee in employees
                    select employee["Employee Designation"].ToString())
                   .Distinct()
                   .ToList();
like image 160
kbrimington Avatar answered Jan 24 '23 01:01

kbrimington