KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I am trying to figure out if I can use LINQ to provide me with the distinct values of some data I have in a DataTable (FirstName, LastName, QTY). I can get the distinct values and fill my List, but I have to run two different LINQ queries to get it....I am sure there is a better way to do it :) Any suggestions would be greatly appreciated (very new to LINQ) Code: public static List<StudentData> LinqDistinct(DataTable dt) { DataTable linqTable = dt; //get the distinct values var query = (from names in dt.AsEnumerable() select new { FirstName = names.Field<string>("FirstName"), LastName = names.Field<string>("LastName") }).Distinct(); //fill my list with the distinct values List<StudentData> sList = (from sa in query.AsEnumerable() select new StudentData { FirstName = sa.FirstName, LastName = sa.LastName //Qty = names.Field<int>("Qty") }).ToList(); return sList;}
Tags (comma-separated)
Save Edits
Cancel