简体   繁体   中英

Entity Framework query error?

I'm getting this error:

The entity or complex type 'Model.Members' cannot be constructed in a LINQ to Entities query.

with my code:

public List<Members> getTeamMembers(String tem_reference)
{
        var query = from c in cecbContext.Projects
                    join b in cecbContext.TeamMembers on c.proj_team equals b.team_reference
                    join d in cecbContext.Members on b.mem_reference equals d.mem_reference
                    where c.proj_reference == tem_reference
                    select new Members
                    {
                        mem_reference = d.mem_reference
                    };
    return query.ToList<Members>();
}

I believe you're running into problems because you're trying to project a mapped entity, and this answer would tell you more: https://stackoverflow.com/a/5325861/2208058

This is what I think might work for you:

var query = from c in cecbContext.Projects
                join b in cecbContext.TeamMembers on c.proj_team equals b.team_reference
                join d in cecbContext.Members on b.mem_reference equals d.mem_reference
                where c.proj_reference == tem_reference
                select d.mem_reference;
return query.Select(ref => new Members { mem_reference = d.mem_reference  }).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM