C# 如何从linq组中的数据表中选择多列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12128985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:10:13  来源:igfitidea点击:

How to select multiple columns from datatable in linq group by?

c#linq

提问by ar.gorgin

I have a DataTable, and i want to grouped with a column .

我有一个数据表,我想与列分组。

I use this code for it :

我使用此代码:

DataTable  dtList = list1.Items.GetDataTable();
         var grouped = from row in dtList.AsEnumerable()
                      group row by row["T"] into valueGroup
                      select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };

My problem is : it is select two column(id,value). I want select multiply column.

我的问题是:选择两列(id,value)。我想选择乘法列。

How to select other column of dtlist ?

如何选择 dtlist 的其他列?

采纳答案by Lukasz M

If you want to select multiple columns to be groupped by, try something like this:

如果要选择要分组的多个列,请尝试以下操作:

DataTable  dtList = list1.Items.GetDataTable();
var grouped = from row in dtList.AsEnumerable()
                      group row by new { T = row["T"], U = row["U"] } into valueGroup
                      select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };

This will give you multiple columns returned as an anonymous type objects with Tand Uproperties accessible in the Valuefield of the return set.

这将为您提供作为匿名类型对象返回的多个列,T并且在返回集UValue字段中具有可访问的属性。

I've tested this and it seems to work on a sample data set.

我已经对此进行了测试,它似乎适用于示例数据集。

It's based on a solution described here: http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/54f6ac70-e74c-4d9d-a9ee-6373714c0755.

它基于此处描述的解决方案:http: //social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/54f6ac70-e74c-4d9d-a9ee-6373714c0755

Edit

编辑

When using group byclause all items are divided into groups and for each group only one item is returned, so in order to refer to a column, you have to either put it in the group byclause or use an aggregation method on it (like you did with IDfield and Max()method).

当 usinggroup by子句时,所有项目都被分成几组,并且每个组只返回一个项目,因此为了引用一列,您必须将它放在group by子句中或对其使用聚合方法(就像您对ID字段所做的那样)和Max()方法)。

However, if you want to make the groups, get the maximum IDfor each group and then retrieve other columns for those items(with maximum IDvalue in each group) you can use a joinwith the same table. This code shows how to do it in your case for sample Tand Ucolumns. It seems to work correctly on a test data set:

但是,如果要创建组,请获取ID每个组的最大值,然后检索这些项目的其他列ID每个组中的最大值),您可以将 ajoin用于同一个表。此代码显示了如何在您的示例TU列的情况下执行此操作。它似乎在测试数据集上正常工作:

var dtListEnumerable = dtList.AsEnumerable();
//group items and get max ID for each group
var groupedTemp = from row in dtListEnumerable
          group row by row["T"] into valueGroup
          select new { Value = valueGroup.Key, ID = valueGroup.Max(id => id["ID"]) };

//join to get the rest of columns for each item with max ID returned for each group
var grouped = from groupedItem in groupedTemp
          join row in dtListEnumerable on groupedItem.ID equals row["ID"]
              into tablesJoined
          from row in tablesJoined.DefaultIfEmpty()
          select new
          {
              ID = groupedItem.ID,
              T = row["T"], 
              U = row["U"]
          };

If you need any help with this, let me know. If it does not return data you want to retrieve, please give some more details about it.

如果您需要任何帮助,请告诉我。如果它没有返回您要检索的数据,请提供有关它的更多详细信息。

回答by Hassan Boutougha

to select multiple column (column1, column2, colummn3) that are in your dtList:

选择 dtList 中的多列(column1、column2、colummn3):

DataTable  dtList = list1.Items.GetDataTable();
var grouped = from row in dtList.AsEnumerable()
                  group row by row["T"] into valueGroup
                  select new {
                         Value = valueGroup.Key,
                         ID = valueGroup.Max(id => id["ID"]),
                         col1 = row["column1"].ToString(),
                         col2 = row["column2"].ToString(),
                         col3 = row["column3"].ToString()
                          };