我想做
IList<Sth> sths= Context.Sth.Where(g => g.IsX == true && g.Y.Name== "name").ToList(); //here it successfully compares g.Y.Name
foreach (Sth g in sths)
{
Context.Entry(g).Collection(g=>g.Y).Load(); //the exception is thrown here
this.mylist.Add(g.Y.Id);
}
我已经试过了
using System.Data.Entity;
using System.Linq;
.
g.Y
is不是一个集合,否则你将无法在第一行调用g.Y.Name
。所以你应该使用Reference
而不是收集,或者最好使用Include
代替。例如:
IList<Sth> sths = Context.Sth
.Where(g => g.IsX == true && g.Y.Name== "name")
.Include(g => g.Y)
.ToList();
this.mylist.AddRange(sths.Select(g => g.Y.Id);
但是,如果你想要做的只是获得Id
属性,那么你可以这样做:
this.mylist.AddRange(Context.Sth
.Where(g => g.IsX == true && g.Y.Name== "name")
.Select(g => g.Y.Id));