Group Join Select With Comma Separated

c# entity-framework-core lambda linq

Question

I have a query Like below in .net Core 2.2 with EF.

var data = context.Customer
                    .GroupJoin(context.Orders, c=> c.Id, o => o.CustoerId, (c, o) => new
                    {
                        customer = c,
                        orders= o
                    }).Select(s => new
                    {
                        s.customer.Name,
                        s.customer.Id,
                        AllOrdersRef = s.orders == null ? null : string.Join(", ", s.orders.Select(x => x.UniquRef))
                    });

It gives Error

The LINQ expression 'Count()' could not be translated and will be evaluated locally.

All I want is Comma Separated Value in AllOrdersRef. Also I dont want to use ToList().

1
0
3/18/2019 10:00:11 AM

Popular Answer

You should make join query and then process it's result on client side:

var data = (from c in context.Customer
            join o in context.Orders on c.Id equals o.CustomerId into subs
            from sub in subs.DefaultIfEmpty()
            select new 
            {
                 c.Id,
                 c.Name,
                 UniquRef = sub == null ? null : sub.UniquRef
            }).ToList();

var result = data.GroupBy(x => new { x.Id, x.Name }).Select(x => new 
             {
                 x.Key.Id,
                 x.Key.Name, 
                 AllOrdersRef = (x.Count() == 1 && x.First().UniquRef == null) ? null
                                    : String.Join(", ", x.Select(y => y.UniquRef))
             });
1
3/18/2019 11:34:45 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow