C# 如何从列表中选择不同的项目
我有这门课C# 如何从列表中选择不同的项目,c#,.net,c#-4.0,C#,.net,C# 4.0,我有这门课 class Test { public string Property { get; set; } public string ID { get; set; } public List<string> MyProperty { get; set; } } 类测试 { 公共字符串属性{get;set;} 公共字符串ID{get;set;} 公共列表MyProperty{get;set;} } 我创建了它
class Test
{
public string Property { get; set; }
public string ID { get; set; }
public List<string> MyProperty { get; set; }
}
类测试
{
公共字符串属性{get;set;}
公共字符串ID{get;set;}
公共列表MyProperty{get;set;}
}
我创建了它的一个实例
List<Test> t = new List<Test>() {
new Test() {
ID = "1",
Property = "2"
},
new Test() {
ID = "2",
Property = "3"
},
new Test() {
ID = "2",
Property = "5"
}
};
List t=新列表(){
新测试(){
ID=“1”,
Property=“2”
},
新测试(){
ID=“2”,
Property=“3”
},
新测试(){
ID=“2”,
Property=“5”
}
};
我希望有一个列表,其中包含按ID过滤的不同元素,并且公共列表MyProperty{get;set;}应该用公共字符串属性{get;set;}数据填充
所以最终的结果应该是
List<Test> = {
1. ID = "1",List<MyProperty> = "2"
2. ID = "2",List<MyProperty> = "2"
};
列表={
1.ID=“1”,List=“2”
2.ID=“2”,List=“2”
};
您可以使用GroupBy
和First
删除重复项:
t.GroupBy(x => x.Id)
.Select(g => g.First())
.ToList();
我将使用
GroupBy()
LINQ扩展:
t.GroupBy(x => x.ID)
.Select(x => new Test {
ID = x.Key,
MyProperty = x.Select(y => y.Property).ToList()
})
.ToList();
其中,GroupBy
的参数是要分组的键,因此在您的情况下为ID
然后,选择将它们投影到新的测试中
以下是一些有用的链接:
结果将是:
[
{
"ID": "1",
"MyProperty": [ "2" ],
"Property": null
},
{
"ID": "2",
"MyProperty": [ "3", "5" ],
"Property": null
},
]
其中TestComparer是比较器的实现。
这是我的建议
//测试类的自定义比较器
类ProductComparer:IEqualityComparer
{
//如果测试ID相等,则测试是相等的。
公共布尔等于(测试x,测试y)
{
//检查比较对象是否引用相同的数据。
if(Object.ReferenceEquals(x,y))返回true;
//检查是否有任何比较对象为空。
if(Object.ReferenceEquals(x,null)| | Object.ReferenceEquals(y,null))
返回false;
//检查产品的属性是否相等。
返回x.Id==y.Id;
}
//对于一对对象,If Equals()返回true
//然后GetHashCode()必须为这些对象返回相同的值。
公共int GetHashCode(测试)
{
//检查对象是否为空
if(Object.ReferenceEquals(test,null))返回0;
//如果名称字段不为null,则获取其哈希代码。
int hashId=test.Id==null?0:test.Id.GetHashCode();
//计算测试的哈希代码。
返回hashId;
//应该足够了,但您可以通过某种方式合并其他字段的哈希代码,例如:
//int hashProperty=test.Property==null?0:test.Property.GetHashCode();
//返回hashId^hashProperty;
}
}
List
你的意思是列出MyPropertyList
?您不能使用变量的名称作为类型。请在此处插入示例…:)
t.Distinct(new TestComparer());
// Custom comparer for the Test class
class ProductComparer : IEqualityComparer<Test>
{
// Tests are equal if their IDs are equal.
public bool Equals(Test x, Test y)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(x, y)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
//Check whether the products' properties are equal.
return x.Id == y.Id;
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public int GetHashCode(Test test)
{
//Check whether the object is null
if (Object.ReferenceEquals(test, null)) return 0;
//Get hash code for the Name field if it is not null.
int hashId = test.Id == null ? 0 : test.Id.GetHashCode();
//Calculate the hash code for the test.
return hashId;
//Should be enough, but you can merge hashcodes of other fields in some way, for example:
//int hashProperty = test.Property == null ? 0 : test.Property.GetHashCode();
//return hashId ^ hashProperty;
}
}