Grouping a generic list via LINQ in VB.NET

前端 未结 1 401
一个人的身影
一个人的身影 2021-01-21 07:50

I need to take a collection and group it via Linq but all the examples I\'ve seen fail in some manner or other with some syntax difference that I can\'t quite lick.

My c

相关标签:
1条回答
  • 2021-01-21 08:47

    Here is the linq code I would write (in c# - sorry I couldn't complete it in VB)

    var res = a.GroupBy(i=>i.EmailAddress )
        .Select(g=> new KeyValuePair<string, string>(
                        g.Key, 
                        string.Join(",", g.Select(x=> x.AlertType ).ToArray()) 
                ));
    

    This will give you the output you want in a collection of KeyValuePair

    in order to group them by both LoadNumber and EmailAddress this would be the c# code:

    var res = alerts
        .GroupBy(a => new { L = a.LoanNumber, E = a.EmailAddress })
        .Select(a => new
            {
                LoadNumber = a.Key.L,
                EmailAddress = a.Key.E,
                Types = string.Join(", ", a.Select(x => x.AlertType).ToArray())
            }).ToList();
    

    where result is a List of a Complext type {LoadNumber, EmailAddress, Types}

    0 讨论(0)
提交回复
热议问题