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

Linq判断List<List<T>>中的重复项
导入Excel之前要检查一遍,看是否有重复的行等错误,多个模块有导入功能,我就想干脆写下面这样一个类,用来记录待导入文件中的错误:
C# code


    public class ImportLog
    {
        /// <summary>
        /// Excel行号
        /// </summary>
        public int RowID { get; set; }

        /// <summary>
        /// 当前行中每个单元格的值
        /// </summary>
        public List<object> FieldList { get; set; }

        /// <summary>
        /// 是否包含错误
        /// </summary>
        public bool HasError { get; set; }

        /// <summary>
        /// 错误信息
        /// </summary>
        public string ErrorMsg { get; set; }
    }


先拿“检查重复的行”来试试:
C# code


        public List<ImportLog> GetRepeatedRows(List<ImportLog> list = null)
        {
            if (list == null)
            {
                list = new List<ImportLog>()
                {
                    new ImportLog(){ RowID=1, FieldList=new List<object>(){"1","11","111"}, ErrorMsg=""},
                    new ImportLog(){ RowID=2, FieldList=new List<object>(){"1","11","111"}, ErrorMsg=""},
                    new ImportLog(){ RowID=3, FieldList=new List<object>(){"1","11","111","1111"}, ErrorMsg=""},
                    new ImportLog(){ RowID=4, FieldList=new List<object>(){"1","11","111","1112"}, ErrorMsg=""},
                    new ImportLog(){ RowID=5, FieldList=new List<object>(){"1","11","112","1121"}, ErrorMsg=""},
                    new ImportLog(){ RowID=6, FieldList=new List<object>(){"1","11","112","1121"}, ErrorMsg=""},
                    new ImportLog(){ RowID=7, FieldList=new List<object>(){"1","12","121","1211"}, ErrorMsg=""},
                    new ImportLog(){ RowID=8, FieldList=new List<object>(){"1","12","122","1221"}, ErrorMsg=""},
                    new ImportLog(){ RowID=9, FieldList=new List<object>(){"1","13","131","1311"}, ErrorMsg=""},
                    new ImportLog(){ RowID=10, FieldList=new List<object>(){"2","21","211","2111"}, ErrorMsg=""},
                    new ImportLog(){ RowID=11, FieldList=new List<object>(){"2","21","211","2111"}, ErrorMsg=""},
                    new ImportLog(){ RowID=11, FieldList=new List<object>(){"2","21","211","2111"}, ErrorMsg=""},
                };
            }
            //方式1:成功,但效率貌似不高
            //var ret1 = list.FindAll(a => list.FindAll(b => b.FieldList.Intersect(a.FieldList).Count() == a.FieldList.Count).Count > 1);

            //方式2:group by m.FieldList into g 失败
            var ret2 = (from t in list
                        where (
                         from d in
                             (
                                 from m in list
                                 group m by m.FieldList into g
                                 where g.Count() > 1
                                 select g
                                 )
                         select d.Key
                        ).Contains(t.FieldList)
                        select t).ToList();
            var count = ret2.Count;//count=0 ……

            return ret2;

        }


各位大神,谁能给优化一下方式1,再给改改方式2?
拜谢!

呃,准备下班了,明天上午来了给分,见谅……