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

本文详细对比了C#和VB中LINQ的使用差异,并通过实际案例展示了如何在VB中高效地进行复杂的数据操作,包括数据加载、分组聚合、合并查询等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.Unit, Key Item.Model}
            Into g = Group Select New With {.price1 = (Aggregate p In g Into Max(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),
                                            .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 Class

Public 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 Property
End Class

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值