[英]How do I select a single grouped column from a dataview?
I have a data.table tblWorkList
with multiple columns: RecordNr
, GroupNum
, Section
, SubscriberID
, and quite a few others.我有一个包含多个列的 data.table tblWorkList
: RecordNr
、 GroupNum
、 Section
、 SubscriberID
和其他一些列。
What I need to do is create a dataview or second datatable that is the equivalent of:我需要做的是创建一个相当于以下内容的数据视图或第二个数据表:
SELECT SubscriberID FROM tblWorkList GROUP BY SubscriberID;
I'm doing it in the application because I need this to end up in a dataview that will then be filtered based on multiple user inputs.我在应用程序中这样做是因为我需要它最终出现在一个数据视图中,然后该数据视图将根据多个用户输入进行过滤。 I have that part working.我有那部分工作。 I've spent several hours now beating my head against the inte.net trying to figure out how to do this, but I keep running up against errors in solutions that LOOK like they should work but end up failing spectacularly.我已经花了好几个小时在 inte.net 上苦思冥想,试图弄清楚如何做到这一点,但我一直遇到解决方案中的错误,这些解决方案看起来应该可以工作,但最终却以惊人的失败告终。 Although, that said, I'm VERY inexperienced with LINQ right now, so I'm sure I'm missing something pretty straightforward.尽管如此,我现在对 LINQ 非常缺乏经验,所以我确定我错过了一些非常简单的东西。
(The basic functionality is this: The table contains a list of records to be processed. Basically, I need to take the table full of records, pull the subscriber IDs into a dataview, allow the user to filter that dataview down by a variety of methods (and providing the user a running count of the number of SubscriberID's matching the selected criteria), and when they're done, assign all of the records associated with the resulting SubscriberID
collection to a specific analyst to be processed.) (基本功能是这样的:该表包含要处理的记录列表。基本上,我需要获取充满记录的表,将订阅者 ID 拉入数据视图,允许用户通过各种过滤该数据视图方法(并为用户提供与所选标准匹配的 SubscriberID 数量的运行计数),完成后,将与生成的SubscriberID
集合关联的所有记录分配给要处理的特定分析师。)
All of the methods I've attempted to use to create the list or dataview of SubscriberID values are enclosed in this:我试图用来创建 SubscriberID 值的列表或数据视图的所有方法都包含在以下内容中:
using (DataTable dt = dsWorkData.Tables["tblWorkData"])
The table tblWorkData
contains approximately 23,000 records.表tblWorkData
包含大约 23,000 条记录。
Here are several of my attempts.这是我的几次尝试。
Attempt 1 - Error is尝试 1 - 错误是
Parameter may not be null. Parameter: source'参数可能不是null。参数:source'
var result1 = from row in dt.AsEnumerable()
group row by row.Field<string>("SubscriberID") into grp
select new { SubscriberID = grp.Key };
ShowMessage(result1.Count().ToString());
Attempt 2 - Error is尝试 2 - 错误是
'Cannot implicitly convert anonymous type: string SubscriberID to DataRow' '无法隐式转换匿名类型:字符串 SubscriberID 到 DataRow'
EnumerableRowCollection<DataRow> query =
from row in dt.AsEnumerable()
group row by row.Field<string>("SubscriberID") into grp
select new { SubscriberID = grp.Key };
Attempt 3 - Error is尝试 3 - 错误是
'The [third] name 'row' does not exist in the current context.' “当前上下文中不存在 [第三个] 名称“行”。”
EnumerableRowCollection<DataRow> query2 =
from row in dt.AsEnumerable()
group row by row.Field<string>("SubscriberID") into grp
select row;
Attempt 4 - same error as Attempt 1:尝试 4 - 与尝试 1 相同的错误:
DataTable newDt = dt.AsEnumerable()
.GroupBy(r => new { SubscriberID = r["SubscriberID"] })
.Select(g => g.OrderBy(r => r["SubscriberID"]).First())
.CopyToDataTable();
MessageBox.Show(newDt.Rows.Count.ToString());
Attempt 5 - same error as Attempt 1:尝试 5 - 与尝试 1 相同的错误:
var result = dt.AsEnumerable().GroupBy(row => row.Field<string>("SubscriberID"));
MessageBox.Show(result.Count().ToString());
Attempt 6 - same error as Attempt 1:尝试 6 - 与尝试 1 相同的错误:
var results = dt.AsEnumerable().GroupBy(g => g["SubscriberID"])
.Select(x => x.First());
MessageBox.Show(results.Count().ToString());
So can someone explain what I'm doing wrong here, or at least point me in the right direction?那么有人可以解释我在这里做错了什么,或者至少给我指出正确的方向吗? I don't really care WHICH approach gets used, for the record, as long as there's a way to do this.我真的不在乎使用哪种方法,为了记录,只要有办法做到这一点。
Answer was a pair of comments from NetMage:答案是来自 NetMage 的一对评论:
Your SQL query is really using
GROUP BY
to doDISTINCT
, so just use the LINQDistinct
:dt.AsEnumerable().Select(r => r.Field<string>("SubscriberID") ).Distinct()
.您的 SQL 查询实际上是使用GROUP BY
来执行DISTINCT
,因此只需使用 LINQDistinct
:dt.AsEnumerable().Select(r => r.Field<string>("SubscriberID") ).Distinct()
。PS Your first error implies that
dt
isnull
-source
is the parameter name toAsEnumerable
. PS 你的第一个错误意味着dt
是null
-source
是AsEnumerable
的参数名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.