我在代码中看到一个奇怪的行为,这是一个使用苹果和人的类似例子,但代码基本相同:
List<Apple> apples = ...
var selectableApples = apples.Select(a => new SelectableApple { SelectedByPerson = null, Apple = a });
foreach (Person person in persons)
{
foreach (var unselectedApple in selectableApples.Where(aa => aa.SelectedByPerson == null))
{
if (/*the person satisfies some conditions*/)
{
// This gets executed like 100 times:
unselectedApple.SelectedByPerson = person;
}
}
}
foreach (var selectedApple in selectableApples.Where(aa => aa.SelectedByPerson != null))
{
Unreachable code - the collection is empty... WTF???
}
SelectableApple
类只是一个没有逻辑的普通C#类,以及所有属性的公共getter和setter。
为什么会这样?
提前致谢!
答案 0 :(得分:6)
selectedApples
不是包含对象的集合,它是一个动态创建集合的表达式。这意味着您对对象所做的更改将被丢弃,当您再次循环selectedApples
时,它将从头开始重新创建。
使用ToList
方法将其设为集合:
var selectableApples = apples.Select(a => new SelectableApple { SelectedByPerson = null, Apple = a }).ToList();
答案 1 :(得分:1)
这里有几个问题。第一个是Where语句不生成对象列表。这是一个表达陈述。
表达式语句在运行时进行评估,因此每次运行语句时都会丢弃对生成的对象的更改。信不信由你,这是一个理想的结果。这允许您以更高效和更优雅的方式处理复杂的嵌套for语句。
回答问题的最佳方法是分析您撰写的内容并重新编写一些代码,以便向您展示更好的方法。
在您的代码中:
List<Apple> apples = ...
var selectableApples = apples.Select(a => new SelectableApple { SelectedByPerson = null, Apple = a });
foreach (Person person in persons)
{
foreach (var unselectedApple in selectableApples.Where(aa => aa.SelectedByPerson == null))
{
// This will ideally give all apples to the first person who
// meets the conditions. As such this if condition can be moved
// out side of the above the foreach loop.
if (/*the person satisfies some conditions*/)
{
// This gets executed like 100 times:
unselectedApple.SelectedByPerson = person;
}
}
}
foreach (var selectedApple in selectableApples.Where(aa => aa.SelectedByPerson != null))
{
Unreachable code - the collection is empty... WTF???
}
因此,如果我们重写这段代码,那么if语句就在内循环的一边。您的代码将执行相同的逻辑操作。请注意,这还没有解决问题,但会让您更近一步。以下是代码的外观:
List<Apple> apples = ...
var selectableApples = apples.Select(a => new SelectableApple { SelectedByPerson = null, Apple = a });
foreach (Person person in persons)
{
// Now we can see that since this will all apples to the first person
// who satisfies the below conditions we are still doing to much. And it
// still does not work.
if (/*the person satisfies some conditions*/)
{
foreach (var unselectedApple in selectableApples.Where(aa => aa.SelectedByPerson == null))
{
// This gets executed like 100 times:
unselectedApple.SelectedByPerson = person;
}
}
}
foreach (var selectedApple in selectableApples.Where(aa => aa.SelectedByPerson != null))
{
Unreachable code - the collection is empty... WTF???
}
现在我们已经开始对事物进行分组,以便能够看到更简单的答案。因为if语句意味着只有满足条件的第一个人才能获得所有苹果。因此,让我们摆脱外部的foreach循环并将其浓缩为LINQ。
List<Apple> apples = ...
var selectableApples = apples.Select(a => new SelectableApple { SelectedByPerson = null, Apple = a });
var selectedPerson = persons.Where(p => /*the person satisfies some conditions*/).First()
if(selectedPerson != null)
{
foreach (var unselectedApple in selectableApples.Where(aa => aa.SelectedByPerson == null))
{
// This gets executed like 100 times:
unselectedApple.SelectedByPerson = person;
}
}
foreach (var selectedApple in selectableApples.Where(aa => aa.SelectedByPerson != null))
{
Unreachable code - the collection is empty... WTF???
}
查看上面的代码,我们现在可以看到内部循环只是对原始选择的修改。让我们来看看:
List<Apple> apples = ...
var selectedPerson = persons.Where(p => /*the person satisfies some conditions*/).First()
var selectableApples = apples.Select(a => new SelectableApple { SelectedByPerson = selectedPerson, Apple = a });
foreach (var selectedApple in selectableApples.Where(aa => aa.SelectedByPerson != null))
{
// This should now run provided that some person passes the condition.
}
现在您的代码将按照需要运行,您可以利用LINQ中提供的延迟加载和循环优化。