将列表数据移动到两个差异列表 [英] Move List Data to two diff lists

查看:60
本文介绍了将列表数据移动到两个差异列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List< classa> ,它包含n个元素。我想将列表分成两个差异列表,对项目进行小的更改。



EX:List< classa> obj = new List< classa>();

它有一些元素



i想要将obj中的索引条目移动到List< classa> ; objEven,同时我想将索引可被4整除的条目移动到List< classa> objDiv4。移动时将一个属性值更改为EVEN和Div4。



I have a List<classa> , it contains n elements . I want to separate the lists into two diff lists with small changes to the Item.

EX: List<classa> obj=new List<classa>();
It has some elements

i want to move even index entry in obj to List<classa> objEven , at the same time i want to move entries whose index is divisible by 4 to List<classa> objDiv4. while moving Change one property value to EVEN and Div4.

class Program
   {
       static void Main(string[] args)
       {
           List<classa> obj = new List<classa>();
           List<classa> objEven = new List<classa>();
           List<classa> objDiv4 = new List<classa>();
           int iCounter = 1;
           for (int i = 1; i <= 100; i++)
               obj.Add(new classA { ID = i, Notes = "Notes " + i.ToString() });

           classA temp = new classA();
           foreach (classA lobj in obj)
           {
               temp = lobj;
               if (iCounter % 2 == 0)
               {
                   temp.Notes = "EVEN";
                   objEven.Add(temp);
               }
               temp = lobj;
               if (iCounter % 4 == 0)
               {
                   temp.Notes = "DIV4";
                   objDiv4.Add(temp);
               }
               iCounter++;
           }

           foreach(classA lobj in objEven)
           {
               Console.Write(lobj.Notes+"  ");
           }
           Console.Read();
       }
   }

   class classA
   {
       public int ID { get; set; }

       public string Notes { get; set; }
   }



这里的问题objEven包含带有DIV4注释的元素。


problem here objEven is containing elements with notes as DIV4.

推荐答案

你的问题是,ID为4的项目被检查两次:

如果他们的ID是偶数则一次;如果他们的ID是4的倍数则一次因为2 if语句不相关。



因为每个可以被4分割的ID也可以被2分割,所以我会在第一个中放置第二个if语句,如下所示:





Your problem is that items with IDs which are dividable by 4 gets checked twice:
once if their ID is even and once if their ID is a multiple of 4 because the 2 if statements are unrelated.

Since every ID which is dividable by 4 is also dividable by 2 I'd place second if statement inside the first one like so:


if (iCounter % 2 == 0)
{
  if (iCounter % 4 == 0) // multiples of 4 will enter this scope
  {
      temp.Notes = &quot;DIV4&quot;;
      objDiv4.Add(temp);
  }
  else // other even numbers will enter this one
  {
    temp.Notes = "EVEN";
    objEven.Add(temp);
  }
}





这样,偶数ID的物品进入objDiv4或者ovjEven,而不一定都进入。



That way items with even ID's get into either objDiv4 or ovjEven and necessarily not into both.


首先,因为你使用一个列表,它更容易使用for而不是foreach。

另外我的解决方案是假设你不在乎是否ID是偶数还是没有,只是索引。



如果你想通过索引做,用obj [i] .ID代替i%2和i%4 。



First off since your using a list, its easier to use the for instead of foreach.
Also my solution is assuming your don't care if the ID is even or not, just the index.

If you want to do by index, substitute the i % 2 and i % 4 with obj[i].ID.

classA temp = null;
for (int i = 0; i < obj.Count - 1; i++)
{
    temp = new classA();
    if (i % 2 == 0)
    {
        temp.ID = obj[i].ID;
        temp.Notes = "EVEN";
        objEven.Add(temp);
    }

    temp = new classA();
    if (i % 4 == 0)
    {
        temp.ID = obj[i].ID;
        temp.Notes = "DIV4";
        objDiv4.Add(temp);
    }
}


这篇关于将列表数据移动到两个差异列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆