LINQ到对象 [英] LINQ TO OBJECT
本文介绍了LINQ到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
T[] arr = ...;
var subsets = from m in Enumerable.Range(0, 1<< arr.Length)
select
from i in Enumerable.Range(0, arr.Length)
where (m & (1 << i)) != 0
select arr[i];
谁能向我解释上面写的linq查询.
请说明流程的进行方式.
Can anyone explain to me the linq query written above.
Please explain how the flow will go.
推荐答案
似乎将所有子集(而不是所有排列)都移出了数组.
因此,如果该数组是包含"A","B"和"C"的字符串数组,它将返回四个子集:
"
"A"
"B",
"A","B"
它通过创建一个外部索引数组来完成此操作,该数组包含从0到arr.Length -
1的所有索引(因此在上面的示例中为0、1、2、3.
然后,对于每个索引项,它将从数组中选择一项,其中内部索引(由Enumerable.Range(0, arr.Length)
创建)是外部索引掩码的一部分.
例如,如果外部索引为3,则选择索引1和2.
(注意,我实际上没有运行代码,所以我可能会弄错了.)
希望这会有所帮助,
弗雷德里克(Fredrik)
It looks like it''s getting all the subsets (not all the permutations) out of the array.
So if the array is a string array containing "A", "B" and "C", it''s going to return four subsets:
""
"A"
"B",
"A", "B"
It does this by creating an outer index array containing all the indices from 0 toarr.Length -
1 (so in the above example that''s 0, 1, 2, 3.
Then, for each of those it will select the item from the array where the inner index (as created byEnumerable.Range(0, arr.Length)
) is part of the outer index mask.
For example, if the outer index is 3 then index 1 and two are selected.
(Caveat, I haven''t actually run the code so I might be mistaken).
Hope this helps,
Fredrik
这篇关于LINQ到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文