使用VB .Net在Linq中分组和最小/最大值

时间:2016-06-14 23:24:09

标签: .net vb.net linq

我希望与此处发布的完全相同(http://forums.asp.net/post/5321593.aspx),但使用VB .Net。

var query = services.GroupBy(s => s.SERVICENAME)
            .Select(s => new { ServiceName = s.Key,
                               Min = s.Min(m => m.MINPRICE),
                               Max = s.Max(m => m.MAXPRICE) }).ToList();

那么,如果可能的话,如何使用VB .Net编写相同的查询?

3 个答案:

答案 0 :(得分:2)

试试这个。

Dim query = services _
            .GroupBy(Function(s) s.SERVICENAME) _
            .Select(Function(s) New With { 
                .ServiceName = s.Key, 
                .Min = s.Min(Function(m) m.MINPRICE), 
                .Max = s.Max(Function(m) m.MAXPRICE)}) _
            .ToList()

答案 1 :(得分:1)

您可以使用:

Dim services As new List(Of Service)
services.Add (New Service With { .SERVICENAME = "service1", .MINPRICE = 5, .MAXPRICE = 7 })
services.Add (New Service With { .SERVICENAME = "service1", .MINPRICE = 3, .MAXPRICE = 9 })
services.Add (New Service With { .SERVICENAME = "service1", .MINPRICE = 6, .MAXPRICE = 8 })
services.Add (New Service With { .SERVICENAME = "service2", .MINPRICE = 2, .MAXPRICE = 4 })
services.Add (New Service With { .SERVICENAME = "service2", .MINPRICE = 3, .MAXPRICE = 4 })

Dim query = services.GroupBy(Function(s) s.SERVICENAME).Select(Function(s) New With { 
            Key .ServiceName = s.Key, 
            Key .Min = s.Min(Function(m) m.MINPRICE), 
            Key .Max = s.Max(Function(m) m.MAXPRICE) 
            }).ToList()

'Result is :-----------------------
'service1        3         9
'service2        2         4

答案 2 :(得分:0)

vb中的查询语法可以更加紧凑。

Dim query =
    From s In services
    Group By ServiceName = s.SERVICENAME Into Min(s.MINPRICE), Max(s.MAXPRICE)