We want to run this query via linq:
SELECT p.SURNAME, STRING_AGG(CONVERT(NVARCHAR(max), p.PHONE), ','), STRING_AGG(CONVERT(NVARCHAR(max), p.EMAIL), ',')
FROM PERSON p
GROUP BY p.SURNAME
We have this mapping
Add(new FunctionMapping(typeof(LinqMethods), "StringAgg", 1, "STRING_AGG(CONVERT(NVARCHAR(max), {0}), ',')"));
How can we write linq? The below one obviously does not work because we do not have an access to PHONE/EMAIL
:
var q = from p in db.Person
group p by p.Surname into g
orderby g.Key
select new
{
g.Key,
StringAggForPhone = LinqMethods.StringAgg(p.Phone),
StringAggForEmail = LinqMethods.StringAgg(p.Email)
};
We specifically want to use linq and not TypedLists or stored procedure because the linq is more complex containing joins & filters from other part of the system.