gpt4 book ai didi

c# - 形成两个对象列表联合的最简单方法(包含 Int 和字符串值)

转载 作者:行者123 更新时间:2023-11-30 19:59:50 24 4
gpt4 key购买 nike

我在这里看到了一个类似的问题,有很好的解决方案: Simplest way to form a union of two lists

但这里的问题是,当每个列表中只有一个参数(int 值)时它起作用。我有这个要求组合 5 个不同的列表,其中包含同一类的对象,没有数据冗余,最终列表应该按 int 值的升序排序。

例子:

Class Company   //data Class
{
int companyNo;
string Name;
}

Class CompanyList : List<Company>
{
.................
public CompanyList GetList(int userID)
{
.....
}
}

company 类有一个公共(public)方法返回对应于搜索条件的公司列表,让我们使用 userID。

CompanyList list1 = CompanyList .GetList(userID1);
CompanyList list2 = CompanyList .GetList(userID2);
CompanyList list3 = CompanyList .GetList(userID3);
CompanyList list4 = CompanyList .GetList(userID4);
CompanyList list5 = CompanyList .GetList(userID5);

The solution I implemented is (worked well):

CompanyList _finalList = list1;
*foreach (CompanyList _list in {_list2 ,_list3 ,_list4 ,_list5}) //loop thorugh all other list
{
foreach (Company item in _list)
{
for (int i = 0; i <= _finalList.Count - 1; i++)
{
if (_finalList.Item(i).CompanyNo== item.CompanyNo)
//***EXIT TAKE NEXT LIST - GO TO *
}
if (i == _finalList.Count - 1) //else check end of first list
{
//company no. not yet encountered(new)
int pos = 0;
foreach (Company companyInfo in _finalList) //search for position for new company no.
{
if (companyInfo.CompanyNo> item.CompanyNo)
{
break;
}
else
{
pos = pos + 1; //increment position
}
}
_finalList.Insert(pos, item); 'Add new item
}
}

}

**代码从 VB.Net 转换为 C#。在这里我找不到该行的等效代码片段,因此将其替换为概念。

我不是专业的 C# 程序员,只是想知道是否有更好或更简单的方法来做到这一点?

数据示例:

Input:
list1[0] = {0,"TCS"};
list1[1] = {1,"Infosys"};
list2[0] = {8,"IBM"};
list3[1] = {1,"Infosys"};
list4[0] = {0,"TCS"};
list5[0] = {9,"Accenture"};
list5[1] = {6,"HCL"};

Output:
finalList[0] = {0,"TCS"};
finalList[1] = {1,"Infosys"};
finalList[2] = {6,"HCL"};
finalList[3] = {8,"IBM"};
finalList[4] = {9,"Accenture"};

问候Sj

最佳答案

好的,所以你有一些东西的序列,在你的情况下,“东西”将是 Company,它不会覆盖 object.Equalsobject.HashCode.

所以,像这样的新扩展可能会很有用

public static IEnumerable<T> Union(
this IEnumerable<T> source,
IEqualityComparer<T> comparer,
params IEnumerable<T>[] others)
{
if (comparer == null)
{
comparer = EqualityComparer<T>.Default;
}

var result = source.Distinct(comparer);
foreach(var o in source)
{
if (o == null)
{
continue;
}

result = result.Union(o, comparer);
}

return result;
}

要使此功能以及采用 IEqualityComparer 的其他功能易于使用,您可以将此类添加到您的代码中,

public class EqualityComparerImproved<T> : EqaulityComparer<T>
{
private readonly Func<T, T> equalityComparison;

private readonly Func<T, int> hashGenerator;

private EqualityComparerImproved(
Func<T, T> equalityComparison,
Func<T, int> hashGenerator)
{
this.equalityComparison = equalityComparison;
this.hashGenerator = hashGenerator;
}

public static EqualityComparerImproved<T> Create
Func<T, T> equalityComparison,
Func<T, int> hashGenerator)
{
if (equalityComparison == null)
{
throw new ArgumentNullException("equalityComparison");
}

if (hashGenerator == null)
{
throw new ArgumentNullException("hashGenerator");
}

return new EqualityComparerImproved<T>(
equalityComparison,
hashGenerator);
}

public override bool Equals(T x, T y)
{
return this.equalityComparison(x, y);
}

public override int GetHashCode(T obj)
{
return this.hashGenerator(obj);
}
}

一旦这两个公认冗长的代码就绪,您就可以执行

var output = list1.Union(
EqualityComparerImproved<Company>.Create(
(x, y) => x.companyNo == y.companyNo && x.Name == y.Name,
(obj) =>
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
hash = hash * 23 + obj.companyNo;
hash = hash * 23 + obj.Name.GetHashCode();
return hash;
}
},
list2,
list3,
list4,
list5);

或者如果 companyNo 是唯一键,

var output = list1.Union(
EqualityComparerImproved<Company>.Create(
(x, y) => x.companyNo == y.companyNo,
(obj) => obj.companyNo),
list2,
list3,
list4,
list5);

就足够了。

关于c# - 形成两个对象列表联合的最简单方法(包含 Int 和字符串值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22689858/

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