LINQ aggregate items in a list, concatenate a property

后端 未结 1 1883
慢半拍i
慢半拍i 2021-01-26 22:56

Please be kind to a LINQ dummy... Say I have 8 rows data like this:

+-------------------+
|  ID   WORKTYPEDESC|
+-------------------+
| 1     plumber     |
| 1           


        
相关标签:
1条回答
  • 2021-01-26 23:41

    Just group by Id, use string.Join to aggregate within each group.

    IEnumerable<Worktype> source = ...;
    var query = source.GroupBy(
        x => x.Id,
        (id, g) => new Worktype
        {
            Id = id,
            WorktypeDesc = string.Join(";", g.Select(x => x.WorktypeDesc).OrderBy(x => x))
        });
    
    0 讨论(0)
提交回复
热议问题