我发现这种行为我并不十分了解发生了什么:
public class CorbeilleViewModel
{
public Corbeille Corbeille { get; private set; }
public CorbeilleViewModel(Corbeille corbeille)
{
Corbeille = corbeille;
}
public bool Selected { get; set; }
}
public class Corbeille
{
}
var corbeilles = new List<Corbeille>()
{
new Corbeille(),
new Corbeille()
};
var corbeillesModel = corbeilles.Select(c => new CorbeilleViewModel(c));
if (corbeillesModel.Any())
corbeillesModel.First().Selected = true;
使用这些代码,corbeillesModel.First().Selected
总是false
。调试时,在设置值Selected
之后,corbeillesModel.First()
再次启动了一次。
要获得正确的值,我必须在ToList
之后Select