日期:2014-05-20  浏览次数:20932 次

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();
        }
    }

------解决方案--------------------
引用:
list=list.GroupBy(t=>t.issuercode).Select(t=>t.First()).ToList();


这个最简单
var query = list.GroupBy(x => new { /*在这里放入你视为重复的列,用逗号分割,比如*/ x.IssuerCode, x.LACode }).Select(x => x.First()).ToList();
------解决方案--------------------
参考Distinct和SequenceEqual