LINQ Query Grouping
-
I have two tables, call it part and customer. Part PartId PartDesc 1 Bolt 2 Screw 3 Nut Customer CustomerId PartId 1 1 2 1 3 1 1 2 I want a result that looks like this: PartId CustomerId 1 1, 2, 3 Sorry for the rough example, but how do I flatten multiple records like that into a comma separated list? Thanks for any guidance, cheers, --EA
-
I have two tables, call it part and customer. Part PartId PartDesc 1 Bolt 2 Screw 3 Nut Customer CustomerId PartId 1 1 2 1 3 1 1 2 I want a result that looks like this: PartId CustomerId 1 1, 2, 3 Sorry for the rough example, but how do I flatten multiple records like that into a comma separated list? Thanks for any guidance, cheers, --EA
Try:
SELECT PartId, CustomerIds=
STUFF((SELECT ',' + CONVERT(VarChar(10), CustomerId)
FROM Customer b
WHERE b.PartId = a.PartId
FOR XML PATH('')), 1, 1, '')
FROM Customer a
GROUP BY PartIdNever underestimate the power of stupid things in large numbers --- Serious Sam
-
Try:
SELECT PartId, CustomerIds=
STUFF((SELECT ',' + CONVERT(VarChar(10), CustomerId)
FROM Customer b
WHERE b.PartId = a.PartId
FOR XML PATH('')), 1, 1, '')
FROM Customer a
GROUP BY PartIdNever underestimate the power of stupid things in large numbers --- Serious Sam
I called this LINQ Query grouping in the title but probably should have specified this is a LINQ to Entities query. I think I have it figured out now, thank you though. Cheers, --EA