LINQ查询帮助 [英] LINQ Query Help
问题描述
我有这个疑问它运行联接的书籍,TradingDesks和ProductInfos。该数据是巨大的每本收集的。
I have this query which runs a join on Books, TradingDesks and ProductInfos. The data is huge in each of this collection.
var queryJoin = from b in books.Values
join d in tradingDesks.Values
on b.TradingDeskId equals d.Id
join p in ProductInfos.Values
**on b.Id equals p.RiskBookId**
select new { p, Book = b.Name, TradingDeskName = d.Name };
在突出显示的行(上b.Id等于p.RiskBookId)
,我也想加入喜欢,(上的b其他条件。 ID等于p.RiskBookId || p.RiskBookId == 0)
。如何在这LINQ语法做到这一点。
In the highlighted line (on b.Id equals p.RiskBookId)
, I also want to add another condition like, (on b.Id equals p.RiskBookId || p.RiskBookId == 0)
. How do I do this in this linq syntax.
我想查询像这样
var queryJoin = from b in books.Values
from d in tradingDesks.Values.Where(x => x.Id == b.TradingDeskId)
from p in cachedProductInfos.Values.Where(y => y.RiskBookId == b.Id)
select new { p, Book = b.Name, TradingDeskName = d.Name };
但是,在这种情况下,查询将永远运行下去,我耗尽内存。所以我想取景它以这种方式做一些疯狂:(
But in this case, the query runs forever and I run out of memory. So I guess framing it in this fashion does something crazy :(
任何帮助是AP preciated。
Any help is appreciated.
谢谢 玛尼
推荐答案
在原来的查询,这些电话以Enumerable.Join使用幕后一个哈希表,使事情很快。如果切换到。凡,你没有得到这些哈希的好处。您可以使用明确散列来获得同样的效果。
In the original query, those calls to Enumerable.Join are using a hashtable behind the scenes to make things fast. If you switch to .Where, you don't get those hash benefits. You can use hashing explicitly to get the same effect.
ILookup<int, string> deskNameLookup = tradingDesks.Values
.ToLookup(
d => d.Id,
d => d.Name
);
ILookup<int, ProductInfo> infoLookup = ProductInfos.Values
.ToLookup(p.RiskBookId);
foreach(b in books.Values)
{
foreach(dName in deskNameLookup[b.TradingDeskId])
{
foreach(p in infoLookup[b.Id].Concat(infoLookup[0]))
{
var x = new {p, Book = b.Name, TradingDeskName = dName};
}
}
}
这篇关于LINQ查询帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!