I have the datatable below:
DefectTypeId EntityId DefectId Remark
------------------------------------------------
1 29000 100 Defect
2 29000 100 Defect
3 29000 200 Ok
1 30000 100 Defect
2 30000 150
9 31000 100 Defect
10 31000 100 Defect
12 31000 200 Ok
How to get this table or list with linq?
EntityId Remark
------------------------
29000 Defect, Ok
30000 Defect
31000 Defect, Ok
Thanks in advance
EDIT
I tried this but it doesn't work (Rows are not grouped):
Dim query = dt.Select("Remark IS NOT NULL").AsEnumerable().GroupBy(
Function(r) New With {
.EntityId = r.Field(Of Integer)("EntityId"),
.DefectId = r.Field(Of Integer)("DefectId"),
.Remark = r.Field(Of String)("Remark")
}).[Select](
Function(g) New With {
g.Key.EntityId,
.Remark = String.Join(",", g.Select(Function(i) i("Remark")))
})
Then I tried this:
Dim query = (From row In dt.Select("Remark IS NOT NULL")
Group By EntityId = row.Field(Of Integer)("EntityId"),
DefectId = row.Field(Of Integer)("DefectId"),
Remark = row.Field(Of String)("Remark") Into g = Group) _
.Select(Function(i) New With {
i.EntityId,
.Remark = String.Join(",", i.Remark)
})
It is better but it is not yet the expected result. Here is the result:
EntityId Remark
------------------------
29000 Defect
29000 Ok
30000 Defect
31000 Defect
31000 Ok
I could get what I want using a foreach after but would like to know if it's possible to get the goal (one row per Entity) in a single Linq instruction. Thank you.