0

我有一个包含两列的 DataTable,这两列都是 ID 列表,具有多对多关系:

  资源 ID 属性 ID
  ---------- ------------
  1 1
  1 2
  1 3
  2 1
  2 3
  3 3

ETC...

给定一个 AttributeID 列表,我想获得一个包含所有提供的 AttributeID 的 ResourceID 列表。

我最初想这样做:

    string[] attributes = "....";
    dv.RowFilter = "AttributeID in (" + String.Join(",", attributes) + ")";

    return dv.ToTable(true, "ResourceID").AsEnumerable().Select(x => (int)x[0]).ToList();

但这给了我一个包含任何提供的 AttributeID 的列表。

我的属性列表目前是一个字符串数组,但如有必要可以更改。我的结果集当前作为 ResourceID 列表返回,但这也是可以协商的。

提前致谢!

4

1 回答 1

0

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>();
于 2012-10-05T12:18:55.367 回答