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
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()