I'm currently shifting my data access code from L2S to Entity framework db first. I have problem with queries like following
var emps = (from emp in DataContext.setupEmployees
let contacts = DataContext.setupContacts.Where(x => x.EmployeeID == emp.EmployeeID).Select(x => x.Contact)
select new
{
EmployeeName = emp.EmployeeName,
Contacts = string.Join(", ", contacts.ToArray())
}).ToList();
EF tells me it can't convert the method string.join
into stored expression. One obvious work-around is to bring the objects in memory (ToList, AsEnumerable etc) and then call string.Join
method like
var emps = (from emp in DataContext.setupEmployees
let contacts = DataContext.setupContacts.Where(x => x.EmployeeID == emp.EmployeeID).Select(x => x.Contact)
select new
{
EmployeeName = emp.EmployeeName,
Contacts = contacts
}).ToList().Select(x=>new{
x.EmployeeName,
Contacts = string.Join(", ",x.Contacts)
});
This solution works perfectly but it's just that it's more verbose and I'd have to duplicate select clause in lots of queries that used to work fine in L2S.
My question: is there any way I can avoid writing this ToList
thing and call string.Join
and similar methods in EF query?