我有一个列表如下:
List<Teacher> _teacher= new List<Teacher>();
教师在以下领域:
public class Teacher
{
public string name{ get; set; }
public string age{ get; set; }
public string basic{ get; set; }
public string hra{ get; set; }
}
另一个清单如下:
List<Salary> _salary= new List<Salary>();
薪水有以下字段:
public class Salary
{
public string basic{ get; set; }
public string hra{ get; set; }
}
在特定情况下,列表教师仅使用姓名和年龄填充。我需要从工资列表中填充教师列表中的基本和hra。 我怎样才能做到这一点?
答案 0 :(得分:2)
salary.AddRange(teachers.Select(s=>new Salary{Basic=s.Basic, hra=s.hra}));
答案 1 :(得分:1)
我相信您有一个Teacher
列表和一个Salary
列表,您需要将薪资的详细信息推送到Teacher
类的相关字段。
我认为您可以通过遍历Teacher类然后访问该特定索引处的Salary列表然后分配这些值来实现此目的。 (我希望列表的计数相同,否则你需要一些方法来确定如何映射这些值)
这些内容
for (int indx = 0; indx < lstdevelop.Count; indx++)
{
lstTeacher[indx].Basic = lstSalary[indx].Basic;
//
}
最好使用正确的数据类型,而不是将所有数据类型都设置为string
,与金钱相关的事情可以坚持decimal
答案 2 :(得分:0)
可能是您可以根据教师级别设置工资
List _teacher = new List(); List _salary = new List(); //populate salaries for later used _salary.Add(new Salary { basic = "300", hra = "400", level = "1" }); _salary.Add(new Salary { basic = "500", hra = "700", level = "2" }); _teacher.Add(new Teacher { name = "Teacher 1", age = "34", level = "1", basic = _salary.Single(p=>p.level == "1").basic, //set basic and hra based on teacher's level hra = _salary.Single(p=>p.level == "1").hra, }); _teacher.Add(new Teacher { name = "Teacher 2", age = "47", level = "2", basic = _salary.Single(p => p.level == "2").basic, //set basic and hra based on teacher's level hra = _salary.Single(p => p.level == "2").hra, }); foreach (Teacher t in _teacher) Console.WriteLine(t.name + ", "+t.age + ", " +t.basic + ", " + t.hra);