Linq去除List的重复行
Dc为多个string的集合,里面有Name,IssuerCode,LACode;
List<Dc>中想去掉多个IssuerCode和LACode重复的
比如:List<DC>里面有 peter,I001,LA001,LAcc
jenny,I001,LA001,LAcc
macus,I002,LA002,LAdd
joan,I002,LA002,LAdd
tina,I003,LA003,LAee
luna,I003,LA003,LAee
lucy,I003,LA003,LAee
想得到最后的结果是:peter,I001,LA001,LAcc
macus,I002,LA002,LAdd
tina,I003,LA003,LAee
List.Distinct又不行。菜鸟在此请教各位大侠,这个List该如何去除其中某几个重复项?
[解决办法]
lst.Distinct<DC>();
DC类集成IComparable,IComparer接口,实现比较函数就可以用Distinct<DC>()了
[解决办法]
list=list.GroupBy(t=>t.issuercode).Select(t=>t.First()).ToList();
[解决办法]
见myblog:
http://blog.csdn.net/q107770540/article/details/5784646
[解决办法]
list=list.Distinct(new DcComparer ()).ToList();
public class DcComparer : IEqualityComparer<Dc>
{
public bool Equals(Dc t1, Dc t2)
{
return (t1.IssuerCode== t2.IssuerCode&& t1.LACode== t2.LACode);
}
public int GetHashCode(Dc t)
{
return t.ToString().GetHashCode();
}
}