Just how do I get a distinctive, gotten checklist of names from a DataTable making use of LINQ?

I have a DataTable with a Name column. I intend to create a collection of the one-of-a-kind names gotten alphabetically. The adhering to question overlooks the order by condition.

var names =
    (from DataRow dr in dataTable.Rows
    orderby (string)dr["Name"]
    select (string)dr["Name"]).Distinct();

Why does the orderby not get applied?

2019-05-07 00:11:56
Source Share
Answers: 2

Try the adhering to

var names = (from dr in dataTable.Rows
             select (string)dr["Name"]).Distinct().OrderBy(name => name);

this needs to benefit what you require.

2019-05-17 14:48:49

The trouble is that the Distinct driver does not grant that it will certainly keep the initial order of values.

So your question will certainly require to function similar to this

var names = (from DataRow dr in dataTable.Rows
             select (string)dr["Name"]).Distinct().OrderBy( name => name );
2019-05-08 20:02:33