C# 如何复制两个类似的列表?
我有两张单子。只有一个字段不同。如何相互填写列表C# 如何复制两个类似的列表?,c#,linq,C#,Linq,我有两张单子。只有一个字段不同。如何相互填写列表 [Serializable()] public class Lst1 { public string filed1 { get; set; } public Int16 filed2 { get; set; } . . . public Boolean filed100 { get; set; } } [Serializabl
[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>();
这是你想要的吗
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);
}
}
类Lst1
{
公共字符串filed1{get;set;}
公共字符串filed2{get;set;}
公共字符串filed3{get;set;}
公共字符串filed4{get;set;}
公共字符串filed5{get;set;}
}
Lst2类
{
公共字符串filed1{get;set;}
公共字符串filed2{get;set;}
公共字符串filed3{get;set;}
公共字符串filed4{get;set;}
公共字符串filed5{get;set;}
公共字符串文件6{get;set;}
}
void CopyData()
{
//测试数据
列表Lst1=新列表()
{
新的Lst1()
{
filed1=“1”,
filed2=“2”,
filed3=“3”,
filed4=“4”,
filed5=“5”,
},
新的Lst1()
{
filed1=“6”,
filed2=“7”,
filed3=“8”,
filed4=“9”,
filed5=“10”,
},
};
列表Lst2=新列表();
foreach(Lst1中的var项目)
{
Type type1=item.GetType();
PropertyInfo[]properties1=type1.GetProperties();
var电流=新的Lst2();
Type type2=current.GetType();
PropertyInfo[]properties2=type2.GetProperties();
int k=0;
foreach(PropertyInfo Properties in Properties 1)
{
var value=property.GetValue(项,空);
int n;
bool isNumeric=int.TryParse(value.ToString(),out n);
如果(!isNumeric)
value=“您想要的值”;
属性2[k].SetValue(当前,值);
k++;
}
Lst2添加(当前);
}
}
它将列表1中的所有内容复制到列表2。无需浪费您的时间和金钱,只需两行代码即可完成:
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
}
}
使用AutoMapper;
命名空间控制台App39
{
班级计划
{
静态void Main(字符串[]参数)
{
//用数据填充列表1
var list1=新的list1
{
Field1=“测试”,
字段2=5,
字段3=假,
};
//1)配置自动映射器
初始化(cfg=>cfg.CreateMap());
//2)创建列表2并用列表1中的数据填充
list2list2=Mapper.Map(list1);
//填充额外字段
list2.Field4=新字节[]{1,2,3};
}
}
公共类列表1
{
公共字符串字段1{get;set;}
公共int字段2{get;set;}
公共布尔字段3{get;set;}
}
公共类列表2
{
公共字符串字段1{get;set;}
公共int字段2{get;set;}
公共布尔字段3{get;set;}
公共字节[]字段4{get;set;}//额外字段
}
}
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; }
}
及
然后使用它:
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
});
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
常数I=15;
尝试
{
//初始化第一个列表
List Lst1=新列表();
Init(Lst1_);
//打印出来
控制台写入线(Lst1);
WriteLine(新字符串('-',I));
Lst1.ForEach(x=>Console.WriteLine(x.PropertyList());
WriteLine(新字符串('=',I));
Console.ReadKey();
//初始化第二个列表
List Lst2_u=Lst1.Cast().ToList();//相当于下面两行
//List Lst2=new List().ConvertAll(x=>(Lst1)x);
//Lst2_u0.AddRange(Lst1_0);
//再加一个
Lst2添加(新Lst2
{
filed1=“101”,
filed2=202,
filed100=true,
filed101=“10100”
});
//和打印
控制台写入线(Lst2);
WriteLine(新字符串('-',I));
Lst2.ForEach(x=>Console.WriteLine(x.PropertyList());
WriteLine(新字符串('=',I));
Console.ReadKey();
}
捕获(例外情况除外)
{
Console.WriteLine(例如ToString());
Console.ReadKey();
}
}
私有静态void Init(列表lst_41;
{
对于(int i=1),我能解释更多你想做的吗?你尝试了什么?你的类有100-101个字符串属性吗?为什么这么多?我觉得类所保存的数据可以更好地表示为列表。也许考虑一下,或者甚至只是把它们存储在<代码>字符串[]中。
,看起来这些只是字段和数字,所以数组或列表对我来说更有意义。糟糕的类设计,一对一可能类2应该继承自类1,并且只有一个属性。如果这是您想要的,请在完成时接受它作为答案。此时:“var value=property.GetValue(item,null);”如果像int32这样的数字字段是正确的,但是如果字符串有错误。我不明白你的意思,你只有字符串属性,哪个数字字段?对不起,我说的不好,我的类字段是字符串、数字和布尔值。你的回答是绝对正确的。只有一个例外,如果两个列表中的字段顺序不正确同样,也会出错。如果我们能够按照字段名称对列表进行排序,那就太好了。如下所示:foreach(properties1.OrderBy中的PropertyInfo属性(c=>c.name))
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
});
}
}
}