在不同的列表C#配套项目 [英] C# Matching items in different lists

查看:98
本文介绍了在不同的列表C#配套项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的对象的列表,其中一个一个IQueryable组(卷成的阵列),而另一列表集。在这两组对象共享一个名为ID字段;每个在第二组中的对象的将与在第一组的对象,但并不一定,反之亦然。我需要能够同时处理组(匹配和不匹配)。两个集合的大小是在这种情况下,350之间300和对象(参考,对于在所述第二组中的对象所生成的XML比7K通常不多,所以也许半认为到三分之二该大小的实际存储器所使用的在每个组中的每个对象)。

我的方式有它目前建立是一个for循环,通过一个数组重的IQueryable的一套presentation,使用LINQ语句迭代查询列表的匹配记录集。这需要太多的时间;我跑酷睿i7的RAM 10GB和它采取的任何地方从10秒到2.5分钟,以匹配和比较的对象。任务管理器不显示任何巨大的内存使用情况 - 下25MB阴影。没有我的系统线程被征税无论是。

有没有方法或算法,让我来配对对象每组中有一个时间,从而通过对和无与伦比的对象迭代以更快的步伐?这组对象是8000+这个节目只是一小部分将不得不咀嚼度过每一天,一旦它开始运行...

编辑:这里是code,我实际运行...

 的for(int i = 0; I< draftRecords.Count();我++)
        {
            sRecord纪录=(来自R在sRecords那里r.id == draftRecords.ToArray()[我] .ID选择R).FirstOrDefault();
            如果(纪录!= NULL)
            基于所述sRecord对象的内容的其余部分{//执行的东西与draftRecords元件
 

解决方案

您应该使用的方法,如的 Enumerable.Join 或的 Enumerable.GroupJoin 以匹配来自两个集合的项目。这将远远超过嵌套的for循环做着更快。

由于要键的集合相匹配的第二列表,它可以或可以不存在的项目

群组加入很可能更合适。这看起来是这样的:

  VAR的结果= firstSet.GroupJoin(secondSet,F => f.Id,S => s.Id,(F,SSET)=>新建{一= F ,秒= SSET});

的foreach(在结果VAR匹配)
{
    Console.WriteLine(项目{0}匹配,match.First);
    的foreach(在item.Seconds VAR秒)
        Console.WriteLine({0},第二); //每个第二项匹配,一次一个
}
 

I have two different lists of objects, one of them an IQueryable set (rolled up into an array) and the other a List set. Objects in both sets share a field called ID; each of the objects in the second set will match an object in the first set, but not necessarily vice versa. I need to be able to handle both groups (matched and unmatched). The size of both collections is between 300 and 350 objects in this case (for reference, the XML generated for the objects in the second set is usually no more than 7k, so think maybe half to two-thirds of that size for the actual memory used by each object in each set).

The way I have it currently set up is a for-loop that iterates through an array representation of the IQueryable set, using a LINQ statement to query the List set for the matching record. This takes too much time; I'm running a Core i7 with 10GB of RAM and it's taking anywhere from 10 seconds to 2.5 minutes to match and compare the objects. Task Manager doesn't show any huge memory usage--a shade under 25MB. None of my system threads are being taxed either.

Is there a method or algorithm that would allow me to pair up the objects in each set one time and thus iterate through the pairs and unmatched objects at a faster pace? This set of objects is just a small subset of the 8000+ this program will have to chew through each day once it goes live...

EDIT: Here's the code I'm actually running...

        for (int i = 0; i < draftRecords.Count(); i++)
        {
            sRecord record = (from r in sRecords where r.id == draftRecords.ToArray()[i].ID select r).FirstOrDefault();
            if (record != null)
            { // Do stuff with the draftRecords element based on the rest of the content of the sRecord object

解决方案

You should use a method such as Enumerable.Join or Enumerable.GroupJoin to match items from the two collections. This will be far faster than doing nested for loops.

Since you want to match a collection of keys to an item in the second list which may or may not exist, GroupJoin is likely more appropriate. This would look something like:

var results = firstSet.GroupJoin(secondSet, f => f.Id, s => s.Id, (f,sset) => new {First = f, Seconds = sset});

foreach(var match in results)
{
    Console.WriteLine("Item {0} matches:", match.First);
    foreach(var second in item.Seconds)
        Console.WriteLine("   {0}", second); // each second item matching, one at a time
}

这篇关于在不同的列表C#配套项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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