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

我正在尝试从此 LING 查询中取回 DataTable,但它给了我一个错误

) expected

谁能明白为什么?

4

3 回答 3

3

你想念这个人:

var results = gridViewDataTable.AsEnumerable()
                    .GroupBy(d => d.Field<string>("String1"))
                    .Select(g => g.OrderByDescending(d => d.Field<string>("String2"))>>>>>>>)<<<<.CopyToDataTable();
于 2013-11-01T15:30:53.657 回答
1

我打算编辑您的帖子以使其更具可读性,但后来我意识到通过编辑我有点让问题所在的位置更加明显。每个左括号都需要一个右括号,所以如果你刚刚开始分解不同的部分,它就会变得非常清楚:

// 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();
于 2013-11-01T15:41:32.370 回答
1

这将创建表列表:

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

这将创建具有分组和有序行的单个数据表(您还需要在此处使用 SelectMany):

gridViewDataTable.AsEnumerable()
    .GroupBy(r => r.Field<string>("String1"))
    .SelectMany(g => g.OrderByDescending(r => r.Field<string>("String2"))) //here
    .CopyToDataTable();
于 2013-11-01T15:32:59.577 回答