[英]Dataview.RowFilter - how to get specific result set
I have a DataTable containing two columns, both of which is a list of ID's, with a many-many relationship: 我有一个包含两列的DataTable,这两列都是ID的列表,具有许多关系:
ResourceID AttributeID ---------- ----------- 1 1 1 2 1 3 2 1 2 3 3 3
etc... 等等...
Given a list of AttributeID's, I want to obtain a list of ResourceID's that have ALL the supplied AttributeID's. 给定AttributeID的列表,我想获得具有所有提供的AttributeID的ResourceID的列表。
I initially thought to do this: 我最初以为是这样做的:
string[] attributes = "....";
dv.RowFilter = "AttributeID in (" + String.Join(",", attributes) + ")";
return dv.ToTable(true, "ResourceID").AsEnumerable().Select(x => (int)x[0]).ToList();
but that gives me a list that have ANY of the supplied AttributeID's. 但这给了我一个包含任何提供的AttributeID的列表。
My list of attributes is currently a string array, but that can be changed if necessary. 我的属性列表当前是一个字符串数组,但是可以根据需要更改。 My result set is currently returned as a List of ResourceID's, but that's negotiable too. 我的结果集当前作为ResourceID的列表返回,但这也是可以协商的。
Thanks in advance! 提前致谢!
For anybody who is looking to do the same thing, here's the solution that I found - essentially grouping my result set and counting the number of items in each group to check that it matches the number of items in my list of supplied attributes. 对于任何想做同样事情的人,这是我找到的解决方案-本质上将结果集分组并计算每个组中的项目数,以检查其是否与我提供的属性列表中的项目数匹配。
dv.RowFilter = "AttributeID in (" + String.Join(",", attributes) + ")";
List<int> Resources = (from a in dv.ToTable().AsEnumerable()
group a by (int)a["ResourceID"] into grp
where grp.Count() == attributes.Length
select grp.Key).ToList<int>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.