Entity Framework asp.net mvc5- 无法将 lambda 表达式转换为类型 'string' 因为它不是委托类型

Entity Framework asp.net mvc5- Cannot convert lambda expression to type 'string' because it is not a delegate type

我正在努力

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 不是集合,否则您将无法在第一行调用 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));