-1
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?

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
jakarta
  • 13
  • 6

3 Answers3

3

You miss this guy:

var results = gridViewDataTable.AsEnumerable()
                    .GroupBy(d => d.Field<string>("String1"))
                    .Select(g => g.OrderByDescending(d => d.Field<string>("String2"))>>>>>>>)<<<<.CopyToDataTable();
Klark
  • 8,162
  • 3
  • 37
  • 61
  • Why incorrect answer which even will not compile is being upvoted? `CopyToDataTable` does not exist for `IEnumerable>` – Sergey Berezovskiy Nov 01 '13 at 15:56
  • @lazyberezovsky: True, but it does answer the question which is essentially "can you spot where is it expecting a parenthesis to get past this error?". – Ocelot20 Nov 01 '13 at 16:07
  • @Ocelot20 you can simply delete all code - that also will get you pass this error. You should fix problem instead of replacing it with another problem – Sergey Berezovskiy Nov 01 '13 at 16:24
  • I just tried to emphasize the character where problem occurs. Unfortunately the code tag doesn't support bold tag inside it self so I thought this will show clearly enough where is the problem. – Klark Nov 01 '13 at 16:29
  • @lazyberezovsky: That's not really a valid argument. I'm not saying its not helpful to also point out other issues, but *this question* was about why he's getting a specific error and how to resolve that error. To reverse your argument, you could also provide him an entirely new solution, but then the user will still not have an understanding of where that error message came from. – Ocelot20 Nov 01 '13 at 16:30
  • @Ocelot20 you do understand, that there are solutions which fix error message and do not create new one? – Sergey Berezovskiy Nov 01 '13 at 16:37
1

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();
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • The second one made my group by invalid and returned back all the rows! – jakarta Nov 01 '13 at 15:40
  • @jakarta yes, it returned all rows but in different order - rows grouped by String1 and each group of rows is ordered by String2. Do you want only one group to be selected? If yes, then which one? – Sergey Berezovskiy Nov 01 '13 at 15:49
  • I want to group by String 1, and then order by descending using String 2 but on the rows that were grouped by String 1 – jakarta Nov 01 '13 at 15:52
  • @jakarta you do understand, that ALL rows will be grouped by String1? – Sergey Berezovskiy Nov 01 '13 at 15:53
  • Yes, for example there are 60 rows in the datatable. When I group by String 1 and do not order by anything, it returns 23 rows. But when I use SelectMany Order By Descending it returns all 60 – jakarta Nov 01 '13 at 15:54
  • @jakarta *When I group by String 1 and do not order by anything, it returns 23 rows* - I'm sorry but that is not possible. You cannot use `CopyToDataTable` method on row groups – Sergey Berezovskiy Nov 01 '13 at 15:58
1

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();
Ocelot20
  • 10,510
  • 11
  • 55
  • 96