簡體   English   中英

如何復制兩個相似的列表?

[英]How do I copy two similar lists?

我有兩個清單。 只有一個場差異。 如何互相填寫清單。

  [Serializable()] 
  public class Lst1
  {
        public string filed1 { get; set; }
        public Int16 filed2 { get; set; }
        .
        .
        .
        public Boolean filed100 { get; set; } 
  }

  [Serializable()] 
  public class Lst2
  {
        public string filed1 { get; set; }
        public Int16 filed2 { get; set; }
        .
        .
        .
        public Boolean filed100 { get; set; } 
        public string filed101 { get; set; }  
  }

List<Lst1> Lst1_ = new List<Lst1>();
List<Lst2> Lst2_ = new List<Lst2>();

我從文件中填寫列表。 然后,我需要從列表一中填寫列表二,有很多字段,而且我不想使用foreach循環。

應當記住,我以前的課程已經構建,序列化並存儲在文件中。 現在,我需要將先前的信息傳遞給第二類結構。

我不想使用此循環!

foreach (var t in Lst1_)
            {
                Lst2_.Add(new lst2
                {
                    filed1 = t.filed1,
                    filed2 = t.filed2,
                    .
                    .
                    .
                    filed100 = t.filed100,
                    filed101 = "kk"
                }
            }

這是你想要的嗎?

class Lst1
{
    public string filed1 { get; set; }
    public string filed2 { get; set; }
    public string filed3 { get; set; }
    public string filed4 { get; set; }
    public string filed5 { get; set; }
}

class Lst2
{
    public string filed1 { get; set; }
    public string filed2 { get; set; }
    public string filed3 { get; set; }
    public string filed4 { get; set; }
    public string filed5 { get; set; }
    public string filed6 { get; set; }
}

void CopyData()
{
        // test data
        List<Lst1> Lst1_ = new List<Lst1>()
        {
            new Lst1()
            {
                filed1 = "1",
                filed2 = "2",
                filed3 = "3",
                filed4 = "4",
                filed5 = "5",
            },
            new Lst1()
            {
                filed1 = "6",
                filed2 = "7",
                filed3 = "8",
                filed4 = "9",
                filed5 = "10",
            },
        };

        List<Lst2> Lst2_ = new List<Lst2>();

        foreach (var item in Lst1_)
        {
            Type type1 = item.GetType();
            PropertyInfo[] properties1 = type1.GetProperties();

            var current = new Lst2();
            Type type2 = current.GetType();
            PropertyInfo[] properties2 = type2.GetProperties();

            int k = 0;
            foreach (PropertyInfo property in properties1)
            {
                var value = property.GetValue(item, null);

                int n; 
                bool isNumeric = int.TryParse(value.ToString(), out n); 
                if (!isNumeric) 
                    value = "Your desired value"; 

                properties2[k].SetValue(current, value);
                k++;
            }

            Lst2_.Add(current);
        }
}

它將所有內容從列表1復制到列表2。

無需浪費時間和金錢, AutoMapper只需兩行代碼即可為您做到:

using AutoMapper;

namespace ConsoleApp39
{
    class Program
    {
        static void Main (string[] args)
        {
            // fill list1 with data
            var list1 = new List1
            {
                Field1 = "test",
                Field2 = 5,
                Field3 = false,
            };

            // 1) configure auto mapper
            Mapper.Initialize (cfg => cfg.CreateMap<List1, List2> ());

            // 2) create list2 and fill with data from list1
            List2 list2 = Mapper.Map<List2> (list1);

            // fill extra fields
            list2.Field4 = new byte[] { 1, 2, 3 };
        }
    }

    public class List1
    {
        public string Field1 { get; set; }
        public int    Field2 { get; set; }
        public bool   Field3 { get; set; }
    }

    public class List2
    {
        public string Field1 { get; set; }
        public int    Field2 { get; set; }
        public bool   Field3 { get; set; }

        public byte[] Field4 { get; set; } // extra field
    }
}

Lst1可以繼承Lst2嗎?

像這樣的東西,兩個列表:

[Serializable()]
public class Lst1
{
    public string filed1 { get; set; }
    public int filed2 { get; set; }
    public bool filed100 { get; set; }
}

[Serializable()]
public class Lst2 : Lst1
{
    public string filed101 { get; set; }
}

一個打印擴展名

public static class CExtensions
{
    public static string PropertyList(this Lst1 obj)
    {
        var props = obj.GetType().GetProperties();
        var sb = new StringBuilder();
        foreach (var p in props)
        {
            sb.AppendLine(p.Name + ": " + p.GetValue(obj, null));
        }
        return sb.ToString();
    }
}

然后使用它:

class Program
{
    static void Main(string[] args)
    {
        const int I = 15;
        try
        {
            //init first list
            List<Lst1> Lst1_ = new List<Lst1>();
            Init(Lst1_);

            //print it
            Console.WriteLine("Lst1_");
            Console.WriteLine(new string('-', I));
            Lst1_.ForEach(x => Console.WriteLine(x.PropertyList()));
            Console.WriteLine(new string('=', I));
            Console.ReadKey();

            //init second list
            List<Lst1> Lst2_ = Lst1_.Cast<Lst1>().ToList(); //equivalent of two next lines
            //List<Lst1> Lst2_ = new List<Lst2>().ConvertAll(x => (Lst1)x);
            //Lst2_.AddRange(Lst1_);

            //add one more
            Lst2_.Add(new Lst2
            {
                filed1 = "101",
                filed2 = 202,
                filed100 = true,
                filed101 = "10100"
            });

            //and print 
            Console.WriteLine("Lst2_");
            Console.WriteLine(new string('-', I));
            Lst2_.ForEach(x => Console.WriteLine(x.PropertyList()));
            Console.WriteLine(new string('=', I));
            Console.ReadKey();


        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadKey();
        }
    }

    private static void Init(List<Lst1> lst_)
    {
        for (int i = 1; i <= 3; i++)
        {
            lst_.Add(new Lst1
            {
                filed1 = i.ToString(),
                filed2 = 2 * i,
                filed100 = i % 2 == 0
            });
        }
    }
}

享受=)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM