سؤال

var results = gridViewDataTable.AsEnumerable()
                    .GroupBy(d => d.Field<string>("String1"))
                    .Select(g => g.OrderByDescending(d => d.Field<string>("String2")).CopyToDataTable();

I am trying to get back a DataTable from this LING query but it gives me an error

) expected

Can anyone see why?

هل كانت مفيدة؟

المحلول

You miss this guy:

var results = gridViewDataTable.AsEnumerable()
                    .GroupBy(d => d.Field<string>("String1"))
                    .Select(g => g.OrderByDescending(d => d.Field<string>("String2"))>>>>>>>)<<<<.CopyToDataTable();

نصائح أخرى

This will create list of tables:

gridViewDataTable.AsEnumerable()
    .GroupBy(r => r.Field<string>("String1"))
    .Select(g => g.OrderByDescending(r => r.Field<string>("String2"))
                  .CopyToDataTable()); // bracket is missing here

This will create single datatable with grouped and ordered rows (you also will need to use SelectMany here):

gridViewDataTable.AsEnumerable()
    .GroupBy(r => r.Field<string>("String1"))
    .SelectMany(g => g.OrderByDescending(r => r.Field<string>("String2"))) //here
    .CopyToDataTable();

I was going to edit your post to make it more readable, but then I realized by editing I was kind of making it more obvious where the issue was. Every opening parenthesis needs a closing one, so if you just start to break out the different pieces it becomes quite clear:

// Please don't actually format your code this way...
var results = gridViewDataTable.AsEnumerable()
                  .GroupBy(
                      d => d.Field<string>("String1")
                  )
                  .Select(
                      g => g.OrderByDescending (
                          d => d.Field<string>("String2")
                        )
                  ) // Needed to close out the Select()
                  .CopyToDataTable();

// How I would actually format this (makes it a little easier to
// break down a single line):
gridViewDataTable.AsEnumerable()
                 .GroupBy(d => d.Field<string>("String1"))
                 .Select(g => g.OrderByDescending(d => d.Field<string>("String2")))
                 .CopyToDataTable();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top