Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在对象相交时快速设置属性?_C#_Performance_Linq_Observablecollection - Fatal编程技术网

C# 在对象相交时快速设置属性?

C# 在对象相交时快速设置属性?,c#,performance,linq,observablecollection,C#,Performance,Linq,Observablecollection,当一个对象同时出现在两个列表中时,我在尝试设置属性时遇到性能问题 public class Product { public int Id { get; set; } } public class DerivateProduct : Product { public bool isIntersected{ get; set; } } public class Storage { public Product stProduct { get; set; } } /

当一个对象同时出现在两个列表中时,我在尝试设置属性时遇到性能问题

public class Product
{
    public int Id { get; set; }
}

public class DerivateProduct : Product
{
    public bool isIntersected{ get; set; }
}


public class Storage
{
    public Product stProduct { get; set; }
}


//Approx 10,000 objects
ObservableCollection<DerivateProduct> Products;
//Approx 500 Objects
ObservableCollection<Storage> Storages;
我尝试了以下方法,仅在150毫秒内获得了正确的迭代次数,但我不知道如何在保持相同性能的同时设置我的
isIntersected=true

var intersectedId = Products.Select(cp => cp.Id)
            .Intersect(Storages.Select(b => b.stProduct.Id))
            .ToList();

我真的需要帮助。感谢@mjwills的帮助,5毫秒就可以了

var stPrIds = Storages.Select(b => b.stProduct.Id).ToArray(); 
foreach (var item in products.Where(cp => stPrIds .Contains(cp.Id)).ToList()) 
{ 
item.IsBiocontrol = true; 
} 

你的英语没有大多数海报那么差,别担心;)@MongZhu不,这是我简化代码时犯的错误,我删除了它。事实并非如此hided@LonelyNeuron谢谢:)该集合是使用Nhibernate创建的,并询问DB它大约需要4秒钟,但在执行Where()…All()时需要另外4秒钟query@MongZhu谢谢你的猜测,我已经做了进一步的调查。在执行代码时,似乎没有进行Db或Orm调用(清除调试输出后,什么也没有出现。根据您的猜测,我已经删除了ToList,这是不必要的,但执行时间仍然大约为4秒:(非常感谢您的帮助,这不是我建议的。我建议使用
哈希集
)。
var stPrIds = Storages.Select(b => b.stProduct.Id).ToArray(); 
foreach (var item in products.Where(cp => stPrIds .Contains(cp.Id)).ToList()) 
{ 
item.IsBiocontrol = true; 
}