2016-04-29 253 views
0

我有兩個相同的類類型的列表。如何將一個列表的一個字段值更新爲另一個列表的另一個字段值?

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); 


      } 

所以現在要插入數據的「SetRollNo」字段列表到第一個列表'SetRolNo'字段。

我嘗試像下面,但沒有得到我的意思。

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

所有正在合併數據..

回答

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); 
} 
+0

哦thanq..I內在價值沒有得到這樣的邏輯。 –

相關問題