How to sort order by descending using Linq query in the DataTable?

chokka
chokka used Ask the Experts™
on
How to sort order by descending using Linq query in the DataTable?

DataTable Dt2 populates data .. and has the data.. I am trying to resort the data in the Dt2.

DataTable Dt2 = new DataTable();
            Dt2 = dt.AsEnumerable()
                .GroupBy(r => r.Field<string>("City"))
                .Select(g =>
                {
                    var row = dt.NewRow();
                    row["City"] = g.Key;
                    row["rating"] = g.Average(r => ParseInt32(r.Field<string>("rating")));
                    return row;
                }).CopyToDataTable();


            var results = from r in Dt2.AsEnumerable()                           
                            select new
                            {   
                                City = r.Field<string>("City"),
                                rating = r.Field<int>("rating")
                            };

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Team Lead
Commented:
try use OrderByDescending, like:

.OrderByDescending(r => r.Field<string>("City"))

Open in new window


Applied to:

DataTable Dt2 = new DataTable();
            Dt2 = dt.AsEnumerable()
                .GroupBy(r => r.Field<string>("City"))
                .Select(g =>
                {
                    var row = dt.NewRow();
                    row["City"] = g.Key;
                    row["rating"] = g.Average(r => ParseInt32(r.Field<string>("rating")));
                    return row;
                })
               .OrderByDescending(r => r.Field<string>("City"))
               .CopyToDataTable();

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial