我有,简化后的循环,如下所示:
词典<元组LT; A,G>,小数>结果=新字典<元组LT; A,G>,小数>();
的foreach(在collectionA A中)
的foreach(B B在collectionB)
结果[Tuple.Create(A,(G)b.groupBy)] + =(十进制)Func键(A,B);
有没有一种方法,我可以使用LINQ查询(用 GROUPBY
,总和
和 ToDictionary 为例)? (如建议这个回答到previous问题:Concise方式做一个加等于可能不会被初始化字典元素上操作)
结果
//字典< EventGroupIDLayerTuple,小数> BcEventGroupLayerLosses
使用下面毓秀李的回答,我能够从链接的问题转换该4内胆:
BcEventGroupLayerLosses =新字典< EventGroupIDLayerTuple,小数>();
的foreach(在this.BcEvents.IncludedEvents UWBCEvent EVT)
的foreach(在this.ProgramLayers层LYR)
BcEventGroupLayerLosses.AddOrUpdate(
新EventGroupIDLayerTuple(evt.EventGroupID,LYR)
GetEL(evt.AsIfs,lyr.LimitInMillions,lyr.AttachmentInMillions)
(A,B)=> A + B);
这个单行:
BcEventGroupLayerLosses = this.BcEvents.IncludedEvents
.SelectMany(EVT => ProgramLayers,(EVT,LYR)=>新建{EVT,LYR})
.GroupBy(G =>新建EventGroupIDLayerTuple(g.evt.EventGroupID,g.lyr)
G => GetEL(g.evt.AsIfs,g.lyr.LimitInMillions,g.lyr.AttachmentInMillions))
.ToDictionary(克=> g.Key,克=> g.Sum());
和双方产生相同的结果。
当然也不是特别的可读性,这是一个很好的尝试。谢谢大家对你的帮助!
解决方案 词典<元组LT; A,G>中十进制>词典=
(来自于collectionA
从B在collectionB
组(十进制)Func键(A,B)由Tuple.Create< A,G>(一,b.groupBy))
.ToDictionary(克=> g.Key,克=> g.Sum());
在声明synatax
VAR字典= collectionA
.SelectMany(一个=> collectionB,
(A,B)=>新{A,B})
.GroupBy(G => Tuple.Create(G.A,g.b.groupBy)
G => FUNC(G.A,g.b))
.ToDictionary(克=> g.Key,克=> g.Sum());
I have a loop which, once simplified, looks like this:
Dictionary<Tuple<A,G>,Decimal> results = new Dictionary<Tuple<A,G>,Decimal>();
foreach( A a in collectionA )
foreach( B b in collectionB )
results [Tuple.Create(a, (G)b.groupBy)] += (Decimal) Func(a, b);
Is there a way I can replicate this result using a Linq query (with GroupBy
, Sum
and ToDictionary
for example)? (as suggested in this answer to a previous question: Concise way to do a plus equals operation on a Dictionary element that might not be initialized)
Result
//Dictionary<EventGroupIDLayerTuple, Decimal> BcEventGroupLayerLosses
Using Yuxiu Li's answer below, I was able to convert this 4 liner from the linked question:
BcEventGroupLayerLosses = new Dictionary<EventGroupIDLayerTuple, Decimal>();
foreach( UWBCEvent evt in this.BcEvents.IncludedEvents )
foreach( Layer lyr in this.ProgramLayers )
BcEventGroupLayerLosses.AddOrUpdate(
new EventGroupIDLayerTuple(evt.EventGroupID, lyr),
GetEL(evt.AsIfs, lyr.LimitInMillions, lyr.AttachmentInMillions),
(a, b) => a + b);
into this one liner:
BcEventGroupLayerLosses = this.BcEvents.IncludedEvents
.SelectMany(evt => ProgramLayers, (evt, lyr) => new { evt, lyr })
.GroupBy(g => new EventGroupIDLayerTuple(g.evt.EventGroupID, g.lyr),
g => GetEL(g.evt.AsIfs, g.lyr.LimitInMillions, g.lyr.AttachmentInMillions))
.ToDictionary(g => g.Key, g => g.Sum());
And both yielded identical results.
Granted neither is particularly readable, this was a good experiment. Thanks everyone for your help!
解决方案Dictionary<Tuple<A, G>, decimal> dictionary =
(from a in collectionA
from b in collectionB
group (decimal)Func(a, b) by Tuple.Create<A, G>(a, b.groupBy))
.ToDictionary(g => g.Key, g => g.Sum());
In declarative synatax
var dictionary = collectionA
.SelectMany(a => collectionB,
(a, b) => new { a, b })
.GroupBy(g => Tuple.Create(g.a, g.b.groupBy),
g => Func(g.a, g.b))
.ToDictionary(g => g.Key, g => g.Sum());