c# - Linq,使用 ToLookup 将值投影到不同的命名变量

标签 c# linq

var tmpProjection = myCollection.ToLookup(t => t.SomeBoolValue);
var listOneFinal = tmpProjection[true];
var listTwo = tmpProjection[false];

第一个问题,有没有办法以更短的方式将它分配给 listOne 和 listTwo,我知道我在这里很迂腐,......只是问问。

现在,

var listThree = listTwo.ToLookup(t => t.SomeOtherBoolValue);
var listFourFinal = listThree[false];
var listFiveFinal = listThree[true];

所以在这种情况下,我(最终)只需要 listOneFinal、listFourFinal 和 listFiveFinal——但我在两者之间创建了这个临时的东西……有没有办法减少这个。

我只谈代码方面,不谈性能或代码关键性。

最佳答案

bool 在传达意图方面有点弱。 Int 好一点,enum 最好。

Lookup<int, T> myLookup = myCollection
.ToLookup(t =>
  t.someBoolValue ? 1 :
  t.someOtherBoolValue ? 4 :
  5
);

var listOne = myLookup[1];
var listFour = myLookup[4];
var listFive = myLookup[5];

关于c# - Linq,使用 ToLookup 将值投影到不同的命名变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3899767/

相关文章:

c# - 等待某事发生——异步或同步模型?

c# - .NET Designer 中的 "could not find type System.Collections.Generic.List"是什么?

c# - 如何从 Windows 8.1 商店应用程序运行 .exe 文件?

c# - LINQ 在两次之间和按小时之间进行过滤

c# - 根据Condition读取多个节点

C# - 如何拆分字符串并过滤其中的一些条目

c# - InlineKeyboardButton 在 InlineKeyboardMarkup 中有限制吗? Telegram Bot C#

c# - 如何从数据列表创建 json 结构?

c# - 如何在 Entity Framework 中获取数字字符串列的最大值

c# - Asyncfileupload 在 webusercontrol 中不起作用