I have a query which works fine in Linq to Objects in this fiddle:
var list = from order in orders
join detail in details
on order.id equals detail.order into od
select new { order = order, details = od };
I tried applying the same query when the data is in a database (note I am mapping Linq to Sql manually):
public class dbContext: DbContext {
public DbSet<Order> Orders { get; set; }
public DbSet<Detail> Details { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder oB) {
oB.UseSqlServer("...connection string...");
}
}
using (var db = new dbContext() {
var list = from order in db.Orders
join detail in db.Details
on order.id equals detail.order into orderDetails
select new { order = order, details = orderDetails };
}
The above gives:
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 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
I tried details = orderDetails.ToList()
in the last line but the same error is there. Where should I add the manual client evaluation?
Some background information: the following database query (without the into
) works fine:
var list = from order in db.Orders
join detail in db.Details
on order.id equals detail.order
select new { order = order, detail = detail };
CodePudding user response:
Instead of a join you should declare Navigation Properties and use something like:
var query = from order in db.Orders
select new { order = order, details = order.OrderDetails };
var list = query.ToList();
or simply
var list = db.Orders.Include(o => o.OrderDetails).ToList();