English 中文(简体)
在数据列表列表中查找相交叉的数据记录
原标题:Find intersecting DataRows in a List of DataTables
  • 时间:2012-05-24 12:24:49
  •  标签:
  • c#
  • .net
  • linq

我有一个列表。 我想通过列表列表中的所有行过滤, 以查找列表中每个数据列表中的所有行 。

如果可能,比较必须放在每一行的“ID”列上。

我试图用林克解决此事,但卡住了。

List<DataTable> dataTables = new List<DataTable>();

// fill up the list
List<DataRow> dataRows = 
    dataTables.SelectMany(dt => dt.Rows.Cast<DataRow>().AsEnumerable()).
    Aggregate((r1, r2) => r1.Intersect(r2));

有什么建议吗?

最佳回答

这不是一个简单的问题。这里有一个解决方案(对我来说似乎太复杂了,但有效 ) 。

  • Obtain the Id value from each row using Linq to DataSets
  • Intersect the multiple lists to find all the common values
  • Find a single occurence of a row in all of the rows that have one of the matching ids

要在数据表上使用Linq,请看

你可以从一张桌子上 像这样的桌子上拿到身份证

var ids = dt.AsEnumerable().Select (d => d.Field<int>("ID")).OfType<int>();

和来自多个表格的

var setsOfIds = dataTables.Select (
    t => t.AsEnumerable().Select (x => x.Field<int>("ID")).OfType<int>());

要交叉多个列表, 请尝试 < a href=" https:// stackoverflow. com/ questions/ 1674742/ interex- explex- of- plus- lists- with- im计数- interfect > > this article 。 使用其中的一种方法, 您可以获取所有ID的交叉点 。

使用 Jon Skeet 辅助工具方法

public static class MyExtensions
{
    public static List<T> IntersectAll<T>(this IEnumerable<IEnumerable<T>> lists)
    {
        HashSet<T> hashSet = new HashSet<T>(lists.First());
        foreach (var list in lists.Skip(1))
        {
            hashSet.IntersectWith(list);
        }
        return hashSet.ToList();
    }
}

我们可以写

var commonIds = setsOfIds.InsersectAll();

现在将数据表上的所有行平整, 并用共同的 ID 过滤 :

var rows = dataTables.SelectMany (t => t.AsEnumerable()).Where(
    r => commonIds.Contains(r.Field<int>("ID")));

现在按 ID 分组, 然后对每行进行第一个实例 :

var result = rows.GroupBy (r => r.Field<int>("ID")).Select (r => r.First ());
问题回答

尝试此选项可以找到两个列表之间的交叉点 :

r1.Join(r2, r1 => r1.Id, r2 => r2.Id, (r1, r2) => r1);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签