从 Linq 的数据表中选择不同的行 [英] Select distinct rows from datatable in Linq

查看:23
本文介绍了从 Linq 的数据表中选择不同的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据多列(attribute1_name、attribute2_name)获取不同的行,并使用 Linq-to-Dataset 从数据表中获取数据行.

I am trying to get distinct rows based on multiple columns (attribute1_name, attribute2_name) and get datarows from datatable using Linq-to-Dataset.

我想要这样的结果

attribute1_name    attribute2_name
--------------     ---------------

Age                State
Age                weekend_percent
Age                statebreaklaw
Age                Annual Sales
Age                Assortment

如何精简 Linq-to-dataset?

How to do thin Linq-to-dataset?

推荐答案

如果它不是类型化数据集,那么您可能想要使用 Linq-to-DataSet 扩展方法来做这样的事情:

If it's not a typed dataset, then you probably want to do something like this, using the Linq-to-DataSet extension methods:

var distinctValues = dsValues.AsEnumerable()
                        .Select(row => new {
                            attribute1_name = row.Field<string>("attribute1_name"),
                            attribute2_name = row.Field<string>("attribute2_name")
                         })
                        .Distinct();

确保在代码的开头有一个 using System.Data; 语句,以启用 Linq-to-Dataset 扩展方法.

Make sure you have a using System.Data; statement at the beginning of your code in order to enable the Linq-to-Dataset extension methods.

希望这有帮助!

这篇关于从 Linq 的数据表中选择不同的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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