C# 使用LinQ从列表中获取不同的列表对象
我有一份商品清单C# 使用LinQ从列表中获取不同的列表对象,c#,linq,list,C#,Linq,List,我有一份商品清单 class Item{ public int Year { get; set; } public int QuarterIndex { get; set; } } 如何将列表转换为不同的列表 资料来源: List<Item> items = new List<Item>(){ new Item(){ Year = 2013, QuarterIndex = 1}, new Item(){ Year = 2013, Quarte
class Item{
public int Year { get; set; }
public int QuarterIndex { get; set; }
}
如何将列表转换为不同的列表
资料来源:
List<Item> items = new List<Item>(){
new Item(){ Year = 2013, QuarterIndex = 1},
new Item(){ Year = 2013, QuarterIndex = 2},
new Item(){ Year = 2013, QuarterIndex = 3},
new Item(){ Year = 2013, QuarterIndex = 1}
};
List items=新列表(){
新项目(){Year=2013,quarteIndex=1},
新项目(){Year=2013,quarteIndex=2},
新项目(){Year=2013,quarteIndex=3},
新项目(){Year=2013,quarteIndex=1}
};
结果:
target = new List<Item>(){
new Item(){ Year = 2013, QuarterIndex = 1},
new Item(){ Year = 2013, QuarterIndex = 2},
new Item(){ Year = 2013, QuarterIndex = 3}
};
target=新列表(){
新项目(){Year=2013,quarteIndex=1},
新项目(){Year=2013,quarteIndex=2},
新项目(){Year=2013,quarteIndex=3}
};
这里有一种简单但可能效率较低的方法,它不需要修改类本身:
items = items.GroupBy(i => new { i.Year, i.QuarterIndex })
.Select(g => g.First())
.ToList();
另一种方法是实现自定义的IEqualityComparer
,可用于Distinct
(以及可枚举类中的其他方法):
如果您可以/想要修改原始类,则可以重写Equals
+GetHashCode
:
public class Item
{
public int Year { get; set; }
public int QuarterIndex { get; set; }
public override bool Equals(object otherItem)
{
Item other = otherItem as Item;
if (other == null) return false;
return this.Equals(other);
}
public bool Equals(Item otherItem)
{
if(otherItem == null) return false;
return Year == otherItem.Year && QuarterIndex == otherItem.QuarterIndex;
}
public override int GetHashCode()
{
unchecked
{
int hash = 23;
hash = (hash * 31) + Year;
hash = (hash * 31) + QuarterIndex;
return hash;
}
}
}
然后,Distinct
自动工作:
下面是一种简单但可能效率较低的方法,它不需要修改类本身:
items = items.GroupBy(i => new { i.Year, i.QuarterIndex })
.Select(g => g.First())
.ToList();
另一种方法是实现自定义的IEqualityComparer
,可用于Distinct
(以及可枚举类中的其他方法):
如果您可以/想要修改原始类,则可以重写Equals
+GetHashCode
:
public class Item
{
public int Year { get; set; }
public int QuarterIndex { get; set; }
public override bool Equals(object otherItem)
{
Item other = otherItem as Item;
if (other == null) return false;
return this.Equals(other);
}
public bool Equals(Item otherItem)
{
if(otherItem == null) return false;
return Year == otherItem.Year && QuarterIndex == otherItem.QuarterIndex;
}
public override int GetHashCode()
{
unchecked
{
int hash = 23;
hash = (hash * 31) + Year;
hash = (hash * 31) + QuarterIndex;
return hash;
}
}
}
然后,Distinct
自动工作:
这个代码帮助你
objlist.Where(w => w.ColumnName != "ColumnValue").GroupBy(g => new { g.Value, g.Name }).
Select(s=> new ClassName(s.Key.Value, s.Key.Name)).ToList()
快乐编码:)此代码帮助您
objlist.Where(w => w.ColumnName != "ColumnValue").GroupBy(g => new { g.Value, g.Name }).
Select(s=> new ClassName(s.Key.Value, s.Key.Name)).ToList()
快乐编码:)你应该:1)使项目实现IEquatable
;2) 使用Distinct()您应该:1)使项
实现IEquatable
;2) 使用Distinct()你能把这个添加到fiddler中吗?@user1120418:你是说.NET Fiddle
?这里有一个使用第三种方法的ideone演示:您可以将其更改为使用items=items.Distinct(newitemcarler()).ToList()
来演示第二种方法。你能把它添加到fiddler中吗?@user1120418:你的意思是.NET Fiddle
?这里有一个使用第三种方法的ideone演示:您可以将其更改为使用items=items.Distinct(newitemcarler()).ToList()代码>以演示第二种方法。