我可以使用LINQ和只有一条IEnumerable<T>
/LINQ语句将一个查询拆分为两个查询吗?
我希望避免对IEnumerable<T>
进行两次迭代。例如,是否可以将下面最后两个语句组合在一起,这样allValues就只被遍历一次?
IEnumerable<MyObj> allValues = ...
List<MyObj> trues = allValues.Where( val => val.SomeProp ).ToList();
List<MyObj> falses = allValues.Where( val => !val.SomeProp ).ToList();
发布于 2010-12-29 04:41:46
您可以使用以下命令:
var groups = allValues.GroupBy(val => val.SomeProp);
强制立即求值,如您的示例所示:
var groups = allValues.GroupBy(val => val.SomeProp)
.ToDictionary(g => g.Key, g => g.ToList());
List<MyObj> trues = groups[true];
List<MyObj> falses = groups[false];
发布于 2010-12-29 09:41:50
有些人喜欢字典,但我更喜欢查找,因为键丢失时的行为。
IEnumerable<MyObj> allValues = ...
ILookup<bool, MyObj> theLookup = allValues.ToLookup(val => val.SomeProp);
// does not throw when there are not any true elements.
List<MyObj> trues = theLookup[true].ToList();
// does not throw when there are not any false elements.
List<MyObj> falses = theLookup[false].ToList();
不幸的是,这种方法枚举了两次-一次是创建查找,另一次是创建列表。
如果你真的不需要列表,你可以将其简化为一次迭代:
IEnumerable<MyObj> trues = theLookup[true];
IEnumerable<MyObj> falses = theLookup[false];
发布于 2017-05-04 04:15:27
为方便起见,请复制意大利面扩展方法。
public static void Fork<T>(
this IEnumerable<T> source,
Func<T, bool> pred,
out IEnumerable<T> matches,
out IEnumerable<T> nonMatches)
{
var groupedByMatching = source.ToLookup(pred);
matches = groupedByMatching[true];
nonMatches = groupedByMatching[false];
}
或者在C# 7.0中使用元组
public static (IEnumerable<T> matches, IEnumerable<T> nonMatches) Fork<T>(
this IEnumerable<T> source,
Func<T, bool> pred)
{
var groupedByMatching = source.ToLookup(pred);
return (groupedByMatching[true], groupedByMatching[false]);
}
// Ex.
var numbers = new [] { 1, 2, 3, 4, 5, 6, 7, 8 };
var (numbersLessThanEqualFour, numbersMoreThanFour) = numbers.Fork(x => x <= 4);
https://stackoverflow.com/questions/4549339
复制相似问题