比较c#中的两个列表内容

时间:2014-11-06 11:09:56

标签: asp.net list c#-3.0

         DataTable dtdcnomissing = new DataTable();
        dtdcnomissing.Clear();
        List<int> Original = new List<int>();
        List<int> duplicate = new List<int>();
        dtdcnomissing = objRetailBAL.DCNOMissing(objRetailPL);
        for (int i = 0; i <= dtdcnomissing.Rows.Count - 1; i++)
        {
            string value = dtdcnomissing.Rows[i]["missdcno"].ToString();
            string lastTwoChars = value.Substring(value.Length - 2);
            int orgnal = Convert.ToInt32(value);
            Original.Add(orgnal);
            if (lastTwoChars.ToString() == "01" || lastTwoChars.ToString() == "51")
            {
                for (int j = Convert.ToInt32(value); j < Convert.ToInt32(value) + 50; j++)
                {
                    int delete = Convert.ToInt32(j);
                    duplicate.Add(delete);

                }
            }
        }
        List<int> result = Original.Except(duplicate).ToList();

我有两个列表

     var list1 = new List<int>{5,6,10,50,101,102,103,104,105,106,107,108,109,110.....150,251,255,274};
     var list2 = new List<int>{101,102,103,104,105,106,107,108,109,110,111,112......150,251,252,253,254,255,256,257,.......300};
     var duplicates = list1.Except(list2).ToList();

现在我想输出

    duplicates={5,6,10,50,251,255,274}

我已经尝试使用上面的代码,但是,正确的输出我没有得到。请任何人帮助我。

2 个答案:

答案 0 :(得分:0)

var list1 = new List<int>{5,6,10,50,101,274};
var list2 = new List<int>{101,102,103,104};
var duplicates = list1.Except(list2).ToList();

结果:

duplicates = list{5,6,10,50,274};

这是你想要的吗?

答案 1 :(得分:0)

     DataTable dtdcnomissing = new DataTable();
        dtdcnomissing.Clear();
        List<int> Original = new List<int>();
        List<int> duplicate = new List<int>();
        List<int> result = new List<int>();
        dtdcnomissing = objRetailBAL.DCNOMissing(objRetailPL);         
        for (int i = 0; i <= dtdcnomissing.Rows.Count - 1; i++)
        {
            string value = dtdcnomissing.Rows[i]["missdcno"].ToString();
            string lastTwoChars = value.Substring(value.Length - 2);
            int orgnal = Convert.ToInt32(value);
            Original.Add(orgnal);
            if (lastTwoChars.ToString() == "01" || lastTwoChars.ToString() == "51")
            {
                int rownum = i + 49;
                if (dtdcnomissing.Rows.Count > i + 49)
                {
                    int str = Convert.ToInt32(dtdcnomissing.Rows[rownum]["missdcno"].ToString());
                    if (Convert.ToInt32(dtdcnomissing.Rows[rownum]["missdcno"].ToString()) == Convert.ToInt32(Convert.ToInt32(value) + 49))
                    {
                        for (int j = Convert.ToInt32(value); j < Convert.ToInt32(value) + 50; j++)
                        {
                            int delete = Convert.ToInt32(j);
                            duplicate.Add(delete);
                        }
                    }
                }
            }
            result = Original.Except(duplicate).ToList();
        }

使用此代码我得到了所需的输出。