使用linq(或任何其他方法)的数据表组 [英] Datatable group by using linq (or any other approach)

查看:87
本文介绍了使用linq(或任何其他方法)的数据表组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下数据:



名称地址
名称1地址1
名称2地址2
姓名3地址1
姓名4地址3
姓名5地址2
姓名6地址4





我希望上表如下:



名称地址
名称1地址1
名称3
名称2地址2
名称5
姓名4地址3
姓名6地址4





我尝试过:



我试过这样的:



 dt = dt.AsEnumerable()。OrderBy(r => ; r.Field< string>(Craft))。CopyToDataTable(); 





但它只能在下表中得到:< br $>


名称地址
名称1地址1
名称3地址1
名称2地址2
名称5地址2
Name4 Address3
Name6 Address4





然后排序名称(根据地址)

和地址值应显示在名称旁边的中间(仅一次)仅在地址栏下)

删除地址栏中的重复值赞:





姓名地址
姓名1地址1
姓名3
姓名2地址2
姓名5







这就是我想要的

解决方案

这不是一个组:分组将信息压缩成摘要,而不是将它们收集在一起。这可能有助于您理解这个想法 - 它适用于SQL查询,但它;完全相同的原则: SQL GROUP By并且列'名称'在选择列表中无效,因为......错误 [ ^ ]



什么你要做的是对你想要订单的记录进行排序,而不是对它们进行分组:

 dt = dt.AsEnumerable()。OrderBy(r => r。字段<串GT;( 地址))CopyToDataTable(); 


I have a datatable like below:

Name        Address
Name1       Address1
Name2       Address2
Name3       Address1
Name4       Address3
Name5       Address2
Name6       Address4



I want the above table like:

Name        Address
Name1       Address1
Name3       
Name2       Address2
Name5       
Name4       Address3
Name6       Address4



What I have tried:

I have tried like this:

dt = dt.AsEnumerable().OrderBy(r => r.Field<string>("Craft")).CopyToDataTable();



but it's only getting this below table:

Name        Address
Name1       Address1
Name3       Address1
Name2       Address2
Name5       Address2
Name4       Address3
Name6       Address4



and then sort Names (according to Address)

and the Address value should display in middle(only once) adjacent to names (under address column only)

remove the duplicate values in Address column Like:



Name      Address
Name1       Address1
Name3       
Name2       Address2
Name5        




this is what i want

解决方案

That's not a Group: grouping condenses information into summaries, not collects them together. This may help you understand the idea - it's for SQL queries, but its; exactly the same principle: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]

What you are trying to do is sort the records, not group them, for which you want OrderBy:

dt = dt.AsEnumerable().OrderBy(r => r.Field<string>("Address")).CopyToDataTable();


这篇关于使用linq(或任何其他方法)的数据表组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆