gpt4 book ai didi

c# - 减少 .NET Core 中常见的列表属性比较次数

转载 作者:行者123 更新时间:2023-12-04 09:16:23 27 4
gpt4 key购买 nike

我有两个一对多关系的表,在这个例子中用 Customers 表示(一)和CustomersInfo (许多)。

using System.Collections.Generic;

namespace Domain.Entities
{
public class CustomersAggregate
{
public CustomersAggregate(int id, string name)
{
Id = id;
Name = name;
}

public int Id { get; set; }
public string Name { get; set; }
public IEnumerable<CustomersInfo> CustomersInfo { get; set; }
}

public class CustomersInfo
{
public CustomersInfo(int customerId, string email)
{
CustomerId = customerId;
Email = email;
}

public CustomersInfo(int customerId, string email, string name)
{
CustomerId = customerId;
Email = email;
Name = name;
}

public int CustomerId { get; set; }
public string Email { get; set; }
public string Name { get; set; }
public CustomersAggregate Customers { get; set; }
}


}
在实际场景中,我必须读取一个包含两列的 .xlsx 文件: NameEmail .要插入到客户表中,我按名称分组,然后选择每个分组中的第一个,然后插入到客户表中。因此,在客户表中,所有名称都是不同的。之后,我再次读取 .xlsx 文件并将已插入客户的名称与文件名称列中读取的值进行比较。如果CustomersInfo.CustomerId 相同,则接收Customers.Id。下面我代表所描述的:
using Domain.Entities;
using System;
using System.Collections.Generic;

namespace Domain
{
public static class Program
{
public static void Main()
{
IEnumerable<Customers> customers = new List<Customers>() {
new Customers(1, "Gustavo"),
new Customers(2, "Lilian"),
new Customers(3, "Ruan"),
new Customers(4, "Zeze"),
new Customers(5, "Augusto"),
new Customers(6, "Maicon"),
new Customers(7, "Nadia")
};

IEnumerable<CustomersInfo> customersInfos = new List<CustomersInfo>()
{
new CustomersInfo(0, "gustavo@costa.com", "Gustavo"),
new CustomersInfo(0, "gustavo@mcosta.com", "Gustavo"),
new CustomersInfo(0, "lilian@costa.com", "Lilian"),
new CustomersInfo(0, "lian@costa.com", "Lilian"),
new CustomersInfo(0, "ruan@co3sta.com", "Ruan"),
new CustomersInfo(0, "ruan@c2osta.com", "Ruan"),
new CustomersInfo(0, "ruan@1costa.com", "Ruan"),
new CustomersInfo(0, "zeze@costa.com", "Zeze"),
new CustomersInfo(0, "zeze@sscosta.com", "Zeze"),
new CustomersInfo(0, "austo@lentini.com", "Augusto"),
new CustomersInfo(0, "austo@lib.com", "Augusto"),
new CustomersInfo(0, "maicon@22coa.com", "Maicon"),
new CustomersInfo(0, "nadia@22c.com", "Nadia"),
};

var ci = new List<CustomersInfo>();

foreach (var customer in customers)
{
foreach (var customerInfo in customersInfos)
{
if (customer.Name == customerInfo.Name)
{
ci.Add(new CustomersInfo(customer.Id, customerInfo.Email));
}
}
}

ci.ForEach(x =>
{
Console.WriteLine(string.Concat(x.CustomerId, " - ", x.Email));
});
}
}
}
问题是 .xlsx 文件有大约 7000 条记录。进行分组选择不同客户的名称后,Customers 表有 3000 行,然后将这些行与文件中的 7000 条记录进行比较,因此最终需要进行 3000 x 7000 次比较,这显然使程序非常减缓。我认为提高性能的一种方法是按字母顺序对客户进行排序,根据客户的中心项目将他们分成两个列表,并仅在这些列表中进行比较。理想情况下,在下面的比较中,如果 customerInfo.Name 以前导字母开头而不是 customerInfo 的首字母,则不会将 customerInfo.Name 与 customer.Name 进行比较。
if (customer.Name == customerInfo.Name)
但是,我不知道如何实现这一点。任何人都可以帮忙吗?

最佳答案

您可以使用 LINQ 的方法 GroupByToDictionary :

var dict = customersInfos
.GroupBy(ci => ci.Name)
.ToDictionary(g => g.Key);
foreach (var customer in customers)
{
if(dict.ContainsKey(customer.Name))
{
ci.AddRange(dict[customer.Name].Select(cInf => new CustomersInfo(customer.Id, cInf.Email)));
}
}
这应该将操作数量从原始 O(customer.Lenth * customersInfos.Length) 减少到 O(customer.Lenth + customersInfos.Length)

关于c# - 减少 .NET Core 中常见的列表属性比较次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63193069/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com