Linq query with subquery as comma-separated values

后端 未结 2 788
無奈伤痛
無奈伤痛 2021-01-05 15:32

In my application, a company can have many employees and each employee may have have multiple email addresses.

The database schema relates the tables like this:

2条回答
  •  离开以前
    2021-01-05 16:15

    Here's now I solved the problem:

    
    from c in Company
    join ex in CompanyEmployeeXref on c.Id equals ex.CompanyId
    join e in Employee on ex.EmployeeId equals e.Id
    join ax in EmployeeAddressXref on e.Id equals ax.EmployeeId
    join a in Address on ax.AddressId equals a.Id
    group a.Email by new {c.Name} into g
    select new {
                    Company=g.Key.Name,
                    Email=g.Select(e=>e).Distinct()
                }
    ).ToList()
    .Select(l=> 
               new {
                        l.Name,
                        Email=string.Join(",", l.Email.ToArray())
                    }
            )
    
    
    

提交回复
热议问题