Linq Include Select from Navigation Property

c# entity-framework-6 linq


I am using the following in an attempt to locate a single member record associated with a specific organisation;

var dataRow = db.Organisation
    .Include(x => x.Member.Select(m => m.Guid == MemberGuid))
    .Where(x => x.Guid == OrganisationGuid)

However this throws the following error;

"The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path"

The Include path is valid and if I take out ".Select(m => m.Guid == model.MemberGuid)" it returns a collection of members for the organisation which I then have to process to locate the required member.

Is there a way to locate the specific member without returning the whole collection?

1/11/2016 5:36:58 AM

Popular Answer

There are two mistakes in your query. First, you are trying to use .Select to filter the records, you will only get true/false records in the column. You should use .Where instead. Second, .Include accepts a navigation property, not the query.

var dataRow = db.Organisation
    .Include(x => x.Member)
    .Where(x => x.Member.Guid == MemberGuid && x.Guid == OrganisationGuid)

An alternative solution is using join:

var dataRow = (from o in db.Organisation
              join m in db.Member
              on o.MemberGuid equals m.MemberGuid
              where o.Guid == OrganisationGuid && m.Guid == MemberGuid
              select o).FirstOrDefault();
1/11/2016 6:00:59 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow