Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

error including related entity #3825


User avatar
0
fguo created

I am running a similar service as PhoneBook example:

    public ListResultDto<PersonListDto> GetPeople(GetPeopleInput input)
    {
        var people = _personRepository
            .GetAll().Include(p => p.Phones)

//.Include(p => p.Phones.Where(x=>x.Number=="1112242")) .WhereIf( !input.Filter.IsNullOrEmpty(), p => p.Name.Contains(input.Filter) || p.Surname.Contains(input.Filter) || p.EmailAddress.Contains(input.Filter) ) .OrderBy(p => p.Name) .ThenBy(p => p.Surname) .ToList();

        return new ListResultDto<PersonListDto>(ObjectMapper.Map<List<PersonListDto>>(people));
    }

It works fine. Now, I need a condition in the "include" method, so I replaced the above ".Include(p => p.Phones)" with ".Include(p => p.Phones.Where(x=>x.Number=="1112242"))". I got a run-time error:

The property expression 'p => {from Phones x in [p].Phones where ([x].Number=="112242") select [x]}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see <a class="postlink" href="http://go.microsoft.com/fwlink/?LinkID=746393">http://go.microsoft.com/fwlink/?LinkID=746393</a>.

I visited Microsoft link as mentioned in the message, but still no clue.

Can you advise me how to add a condition (or other such as .Distinct(), .First()...) in the .Include method?

Thanks,


2 Answer(s)
  • User Avatar
    0
    alper created
    Support Team

    hi this is completely related with entity framework (not aspnet-zero issue) try to get help from <a class="postlink" href="https://stackoverflow.com/questions/tagged/entity-framework">https://stackoverflow.com/questions/tag ... -framework</a>

  • User Avatar
    0
    fguo created

    Thank you for clarification! I searched online as your suggestion, but it seems no easy workaround so far. The "Include" extension can only work in simple typical cases. I have to remove it from my application for now, and hope the ef core improves this feature soon.

    Thanks again!