简体   繁体   中英

The LINQ expression could not be translated - Entity Framework Core

I am trying to find all departments with more than five employees. This is what I tried:

public static string GetDepartmentsWithMoreThan5Employees(SoftUniContext context)
{
    var departments = context
        .Departments
        .Where(d => d.Employees.Count > 5)
        .Select(d => new
        {
            d.Name,
            ManagerFirstName = d.Manager.FirstName,
            ManagerLastName = d.Manager.LastName,
            Employees = d.Employees
                .Select(e => new
                {
                    e.FirstName,
                    e.LastName,
                    e.JobTitle
                })
                .OrderBy(e => e.FirstName)
                .ThenBy(e => e.LastName)
                .ToList()
        })
        .OrderBy(d => d.Employees.Count)
        .ThenBy(d => d.Name)
        .ToList();

    return "";
}

For some reason, this always results in an exception saying that the LINQ expression could not be translated. My assumption is that this is due to the ordering after the outer select. How can I fix it?

Thank you in advance!

The current answer suggests that the navigation property Department.Employees should be of type List . That's not correct and it misses the point. The point here is almost the opposite.

First, ICollection is perfectly fine for navigation properties. It's used in many examples in the official Entity Framework documentation. Also, ordering by Count (without () ) of such navigation properties works fine. And Count() as well, of course.

The exception message was something like:

The query [query text] could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().

An exception that's far too common at the moment with EF core 3, unfortunately.

The ToList() addition in the subquery is the culprit here. Removing it, and using Count() instead of Count to make it compile again will probably* make the exception go away:

var departments = context
    .Departments
    .Where(d => d.Employees.Count > 5) // Count or Count() is fine here
    .Select(d => new
    {
        d.Name,
        ManagerFirstName = d.Manager.FirstName,
        ManagerLastName = d.Manager.LastName,
        Employees = d.Employees
            .Select(e => new
            {
                e.FirstName,
                e.LastName,
                e.JobTitle
            })
            .OrderBy(e => e.FirstName)
            .ThenBy(e => e.LastName)
    })
    .OrderBy(d => d.Employees.Count()) // Here, the compile-time type of Employees
                                       // is IEnumerable<T>, so Count() must be used
    .ThenBy(d => d.Name)
    .ToList();

*I say "probably" because it works in a similar query I tested, but I don't know OP's class model.

What is the type of Employees in the Department class?

This error can occur when OrderBy is used with a member of type ICollection .

Use List instead.

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