如何将一个列表的一个字段值更新为另一个列表的另一个字段?

时间:2016-04-29 07:22:31

标签: c# entity-framework

我有两个相同类类型的列表。

List<CUSTOM_STU_DETAIL> StuList = new List<CUSTOM_STU_DETAIL>();
List<CUSTOM_STU_DETAIL> StuListforRollNo = new List<CUSTOM_STU_DETAIL>();

所以我从两个表中得到一个列表。

StuList = (from a in db.Student_Re_Admission
                           join b in db.Student_Registration on a.Registration_Id equals b.Registration_Id

                           where a.Academic_Year_Id == AcademicId && a.Company_ID == CompanyId
                           && a.COMPANY_LOCATION_ID == CompanyLocationId
                           && a.Class_Id == ClassID && a.Section_Id == SectionID
                           && a.Promoted == false && b.Delete_Flag == false

                           select new CUSTOM_STU_DETAIL
                           {
                               Registration_Id = a.Registration_Id,
                               Admission_No = a.Admission_No,                               
                               STUDENT_NAME = a.Student_First_Name + " "+a.Student_Middle_Name + " " + a.Student_Last_Name,
                               Class_Id = ClassID,
                               Section_Id = SectionID,
                               Academic_Year_Id = AcademicId,
                               Company_Id = CompanyId,
                               Company_Location_Id = CompanyLocationId

                               //RollNo = c.Set_Roll_No,
                           }).OrderBy(a => a.Admission_No).ToList();

所以现在想要在第一个列表循环中从另一个具有相同条件的表中获取一些值。

foreach (var obj in StuList)
                {
                    var rollNoRecord = db.Student_Roll_No_Assign.FirstOrDefault(a => a.Company_ID == obj.Company_Id && a.COMPANY_LOCATION_ID == obj.Company_Location_Id && a.Academic_Year_Id == obj.Academic_Year_Id && a.Class_Id == obj.Class_Id && a.Delete_Flag == false);

                    if (rollNoRecord != null)
                    {
                        data.RollNo = rollNoRecord.Set_Roll_No;
                    }
                    else
                    {
                        data.RollNo = string.Empty;
                    }

                    StuListforRollNo.Add(data);


            }

所以现在想要插入&#39; SetRollNo&#39;字段列表中的数据列入第一个列表&#39; SetRolNo&#39;字段。

我尝试如下,但没有得到我的意思。

    1) var listfinal = StuList.Concat(StuListforRollNo);
    2) var listfinal = StuList.Union(StuListforRollNo);
    3) var listfinal = StuList.AddRange(StuListforRollNo);

所有人都在合并数据..

1 个答案:

答案 0 :(得分:1)

为什么你不改变foreach循环中的值

foreach (var obj in StuList)
{
     var rollNoRecord = db.Student_Roll_No_Assign.FirstOrDefault(a => a.Company_ID == obj.Company_Id && a.COMPANY_LOCATION_ID == obj.Company_Location_Id && a.Academic_Year_Id == obj.Academic_Year_Id && a.Class_Id == obj.Class_Id && a.Delete_Flag == false);

     if (rollNoRecord != null)
     {
           //data.RollNo = rollNoRecord.Set_Roll_No;
           obj.RollNo = rollNoRecord.Set_Roll_No;
     }
     else
     {
           //data.RollNo = string.Empty;
           obj.RollNo = string.Empty;
     }
     //StuListforRollNo.Add(data);
}