LINQ subquery group by to produce comma delimited results for a specified column


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var secondaryContents = (from c in context.ProductSecondaryContents
  2. join cs in context.ProductSecondaryContentStates on c.ProductSecondaryContentID equals cs.ProductSecondaryContentID
  3. join s in context.States on cs.StateCodeID equals s.StateID
  4. where c.ProductID == ProductID
  5. group s.Code by new {
  6. c.ProductSecondaryContentID,
  7. c.CreateDateTime,
  8. c.GroupName
  9. } into g
  10. select new
  11. {
  12. g.Key.ProductSecondaryContentID,
  13. g.Key.CreateDateTime,
  14. g.Key.GroupName,
  15. State = g.Select(st=>st).Distinct()
  16. }).ToList().Select(l=>
  17. new {
  18. l.GroupName,
  19. ApplicableStates = string.Join(", ", l.State.ToArray()),
  20. l.CreateDateTime,
  21. l.ProductSecondaryContentID
  22. }
  23. );

URL: http://stackoverflow.com/questions/2861202/linq-query-with-subquery-as-comma-separated-values

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.