VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)

VB.NET中LINQ TO List泛型查询语句(分组,聚合函数),第1张

概述Public Class LinqToList 'LINQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样 Dim listNew As List(Of Product) = New List(Of Product) '新商品 Dim listOld As List(Of Product) = New List(Of Product) '旧商品
Public Class linqToList    'liNQ在C#中使用比较方便,但是在VB中使用比较麻烦,复杂,和C#用法并不太一样    Dim ListNew As List(Of Product) = New List(Of Product)  '新商品    Dim Listold As List(Of Product) = New List(Of Product)  '旧商品    '****** 给 ListNew 加载数据 此处省略******    '****** 给 Listold 加载数据 此处省略******    '查询 ListNew 中的最高价 price1,并按 price,name,unit,model,node分组    Dim temp = From Item In ListNew                    Group Item By Key = New With {Key Item.name,Key Item.Unit,Key Item.Model}                    Into g = Group Select New With {.price1 = (Aggregate p In g Into Average(p.Price)),.price = (From p In g Select p.Price),.name = Key.name,.unit = Key.Unit,.model = Key.Model,.note = (From p In g Select p.Note)}    'note并未在分组中,无法再key中获取    '合并ListNew 和Listold ,并按 price,name,unit,model,node分组,求出合并后的最高价price1,相同产品的个数.count    Dim tempMax = From Item In            ((From Contact In ListNew).Union(From Shipment In Listold))            Group Item By Key = New With {Key Item.name,Key Item.Model}            Into g = Group Select New With {.price1 = (Aggregate p In g Into Max(p.Price)),.note = (From p In g Select p.Note),.count = g.Count()}    '最低价 .price1 = (Aggregate p In g Into Max(p.Price)) 改成 .price1 = (Aggregate p In g Into Min(p.Price))     '平均价 .price1 = (Aggregate p In g Into Max(p.Price)) 改成 .price1 = (Aggregate p In g Into Average(p.Price))End ClassPublic Class Product    Private mPrice As Double     '价格    Private mname As String      '名称    Private munit As String      '单位    Private mModel As String     '规格    Private mNote As String      '备注    Public Property Price() As Double   '价格        Get            Return mPrice        End Get        Set(ByVal value As Double)            mPrice = value        End Set    End Property    Public Property name() As String    '名称        Get            Return mname        End Get        Set(ByVal value As String)            mname = value        End Set    End Property    Public Property Unit() As String    '单位        Get            Return munit        End Get        Set(ByVal value As String)            munit = value        End Set    End Property    Public Property Model() As String   '规格        Get            Return mModel        End Get        Set(ByVal value As String)            mModel = value        End Set    End Property    Public Property Note() As String    '备注        Get            Return mNote        End Get        Set(ByVal value As String)            mNote = value        End Set    End PropertyEnd Class
总结

以上是内存溢出为你收集整理的VB.NET中LINQ TO List泛型查询语句分组,聚合函数)全部内容,希望文章能够帮你解决VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1278599.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存