提高Linq到Datatable的性能
本文关键字:性能 Datatable Linq 提高 | 更新日期: 2023-09-27 18:21:56
我有一个500K行的数据表,格式如下;
Int | Decimal | String
我们使用的是单例模式,最终我们的DataTable
需要最终成为List(Of AssetAllocation)
,其中AssetAllocation
是:
Public Class AssetAllocation
Property TpId() As Integer
Property Allocation As List(Of Sector)
End Class
Public Class Sector
Property Description() As String
Property Weighting As Decimal
End Class
我正在使用的linq;
Private Shared Function LoadAll() As List(Of AssetAllocation)
Dim rtn = New List(Of AssetAllocation)
Using dt = GetRawData()
Dim dist = (From x In dt.AsEnumerable Select x!TP_ID).ToList().Distinct()
rtn.AddRange(From i As Integer In dist
Select New AssetAllocation With {
.TpId = i,
.Allocation = (From b In dt.AsEnumerable
Where b!TP_ID = i Select New Sector With {
.Description = b!DESCRIPTION.ToString(),
.Weighting = b!WEIGHT
}).ToList()})
End Using
Return rtn
End Function
执行linq需要很长时间,这是由于内部查询构建了扇区列表。不同列表包含80k
这能得到改善吗?
如果我已经了解您要做什么,那么这个查询应该会有更好的性能。诀窍是使用GroupBy
来避免在每次迭代中都必须在整个表中搜索匹配的id。我用C#写的,但我相信你能把它翻译成VB.
var rtn =
dt.AsEnumerable()
.GroupBy(x => x.Field<int>("TP_ID"))
.Select(x => new AssetAllocation()
{
TpId = x.Key,
Allocation = x.Select(y => new Sector
{
Description = y.Field<string>("Description"),
Weighting = y.Field<decimal>("WEIGHT")
}).ToList()
}).ToList();