博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
VB.NET中LINQ TO List泛型查询语句(分组,聚合函数)
阅读量:5021 次
发布时间:2019-06-12

本文共 3159 字,大约阅读时间需要 10 分钟。

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

 

转载于:https://www.cnblogs.com/huijiBreathe/p/3180667.html

你可能感兴趣的文章
学习总结 javascript 闭包
查看>>
display:flow-root
查看>>
22-reverseString-Leetcode
查看>>
Centos 开机自动联网
查看>>
cocos2dx使用lua和protobuf
查看>>
Codeforces Round #327 (Div. 2)
查看>>
How to install ia32-libs in Ubuntu 14.04 LTS (Trusty Tahr)
查看>>
The Ctrl & CapsLock `problem'
查看>>
Java进阶知识点6:并发容器背后的设计理念 - 锁分段、写时复制和弱一致性
查看>>
Makefile ===> Makefile 快速学习
查看>>
java性能调优工具
查看>>
C# 其他的Url 文件的路径转化为二进制流
查看>>
cmake使用
查看>>
面向对象高级
查看>>
Bitwise And Queries
查看>>
oracle连接问题ORA-00604,ORA-12705
查看>>
Java从零开始学十三(封装)
查看>>
Python2和Python3中的rang()不同之点
查看>>
lintcode28- Search a 2D Matrix- easy
查看>>
A Simple Tree Problem
查看>>