thestruggleisreal
thestruggleisreal

Reputation: 1295

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

I am trying to do

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);
            }

I already tried

using System.Data.Entity;
using System.Linq;

.

Upvotes: 0

Views: 531

Answers (1)

DavidG
DavidG

Reputation: 119076

g.Yis not a collection otherwise you wouldn't be able to call g.Y.Name on the first line. So you should either use Reference instead of collection, or preferably use Include instead. For example:

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);

But, if all you are trying to do it get the Id property, then you can just do that:

this.mylist.AddRange(Context.Sth
    .Where(g => g.IsX == true && g.Y.Name== "name")
    .Select(g => g.Y.Id));

Upvotes: 2

Related Questions