【问题标题】:Grouping and min/max values in Linq using VB .Net使用 VB .Net 在 Linq 中分组和最小/最大值
【发布时间】:2016-06-14 23:24:08
【问题描述】:

我希望执行与此处 (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 编写相同的查询?

【问题讨论】:

    标签: .net vb.net linq


    【解决方案1】:

    试试这个。

    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()
    

    【讨论】:

      【解决方案2】:

      你可以用这个:

      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
      

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-04
          • 2011-01-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-31
          • 1970-01-01
          相关资源
          最近更新 更多