【发布时间】:2017-07-21 13:04:09
【问题描述】:
我有一个场景,我需要根据另一个列表中的数据更新一些项目。我已经在这里解决了各种问题,但没有任何帮助。
场景
listA: 总数约为 88000
public class CDRs
{
public string cld { get; set; }
public string prefix2 { get; set; }
public string country { get; set; }
public string city { get; set; }
}
listB:总数:3000。
public class RatesVM
{
public string prefix { get; set; }
public string Country { get; set; }
public string City { get; set; }
}
现在在 listB 中可以有多个匹配的 listA 字段,即 cld
例如。 listA.cld = "8801123232"; 我得到的 ListB 中匹配的前缀是
880 BGD Proper
8801 BGD Mobile
88011 BGD Dhaka Mobile
88017 BGD Dhaka Mobile
88018 BGD Dhaka Mobile
88019 BGD Dhaka Mobile
现在我想要在这种情况下最接近的匹配是
88011 BGD Dhaka Mobile
我现在正在遵循的方法。
foreach (var x in listA)
{
var tempObj = listB.FirstOrDefault(y => x.cld.StartsWith(y.prefix));
if (tempObj != null)
{
x.prefix2 = tempObj.prefix;
x.country = tempObj.Country;
x.city = tempObj.City;
}
else
{
x.prefix2 = "InBound";
x.country = "Unknown";
x.city = "Unknown";
}
}
它工作正常,但需要很多时间。 本案例大约需要 2-3 分钟。
在少数情况下 ListA 会有大约 100 万条记录。我担心这会花很长时间。
提前致谢
【问题讨论】:
-
为什么需要找到最接近的匹配项?为什么不能直接链接?另外,您在标题中提到了
Contains,但您没有显示代码。 -
@TimSchmelter 我假设他们的意思是 StartsWith 并编辑 em 的标题:P
-
如果将列表拆分为计算机上的核心数并使用 Parallel.Foreach() 方法,速度会更快一些。 --> tutorial
-
这些选项中的任何一个对你有用吗@JitenderSharma ?
-
@mjwills - 事实上,你的是这里最快的,在此之后 Tim-Schmelter 的回答也适用于某些场景,但是当我有 200 万条记录时,它会变慢,这很常见根据我的要求。所以我决定避免这种情况,并且只将数据存储在我需要的 sql 中。
标签: c# asp.net list ienumerable