Lambda and Linq in vb
Sub Main()
' 1. 初始化資料
Dim dep As New datatable
Dim emp As New datatable
Dim tmp0 As New datatable
Dim tmp As New datatable
Dim tmp2 As New datatable
Dim tmp3 As New datatable
dep.columns.add("id",Gettype(Integer))
dep.columns.add("name",Gettype(String))
dep.rows.add(1,"pe")
dep.rows.add(2,"me")
dep.rows.add(3,"ie")
emp.columns.add("id",Gettype(Integer))
emp.columns.add("name",Gettype(String))
emp.columns.add("depId",Gettype(Integer))
emp.rows.add(1,"king",1)
emp.rows.add(2,"lily",1)
emp.rows.add(3,"bob",4)
tmp.columns.add("id",Gettype(Integer))
tmp.columns.add("name",Gettype(String))
tmp.columns.add("depId",Gettype(Integer))
tmp.columns.add("depName",Gettype(String))
tmp0=tmp.clone()
Dim res = emp.asenumerable().Join(
dep.asenumerable,
Function(l) l.Field(Of Integer)("depId"),
Function(r) r.Field(Of Integer)("id"),
Function(g,e) tmp0.loaddatarow(
g.ItemArray.concat({If(e Is Nothing,"no",e("name"))}).toarray(),False
)
)
res.count()
Call "tmp0".Dump()
tmp0.Dump()
res = emp.asenumerable().GroupJoin(
dep.asenumerable,
Function(l) l.Field(Of Integer)("depId"),
Function(r) r.Field(Of Integer)("id"),
Function(l,gg) New With { l, gg}
).SelectMany(
Function(g) g.gg.defaultifempty(),
Function(g,e) tmp.loaddatarow(
g.l.ItemArray.concat({If(e Is Nothing,"no",e("name"))}).toarray(),False
)
)
res.count()
Call "tmp:".Dump()
tmp.Dump()
tmp2=tmp.clone()
Dim res2 = From l In emp.AsEnumerable()
Join r In dep.AsEnumerable()
On l("depId") Equals r("id")
Select tmp2.LoadDataRow(l.ItemArray.Concat({ r("name") }).ToArray(), False)
res2.count()
Call "tmp2:".Dump()
tmp2.Dump()
tmp3=tmp.clone()
Dim res3 = From l In emp.AsEnumerable()
Group Join r In dep.AsEnumerable()
On l("depId") Equals r("id")
Into gg=Group
From s In gg.defaultifempty()
Select tmp3.LoadDataRow(l.ItemArray.Concat({ If(s Is Nothing,"...",s("name")) }).ToArray(), False)
res3.count()
Call "tmp3:".Dump()
tmp3.Dump()
End Sub
评论已关闭