Why is this query not supported in linq for objects?

I have such a request.

var query = from o in _context.Orders
            join c in _context.Customers on o.CustomerId equals c.CustomerId
            orderby o.ShippedDate descending
            select new {OrderId = o.OrderId, Customer = c.FirstName};

I get the error "Only initializers, entities, and entity navigation properties are supported." I swear that I can do it earlier, but for me life cannot understand what the problem is. I also cannot restructure such a request.

var query = from o in _context.Orders
            orderby o.ShippedDate descending
            select new {OrderId = o.OrderId, Customer = o.Customer.FirstName};

Upset.

+3
source share
2 answers

Try the following:

var query = from o in _context.Orders
            join c in _context.Customers on o.CustomerId equals c.CustomerId
            orderby o.ShippedDate descending
            select new {o.OrderId, c.FirstName};
0
source

Try something like ...

 var query = from o in _context.Orders
             join c in _context.Customers on o.CustomerId equals c.CustomerId
             orderby o.ShippedDate descending
             let OrderID = o.OrderID
             let Customer = c.FirstName
             select OrderID, Customer
0
source

All Articles