2017-06-29 84 views
0

我有實體如下。我如何將DataTable轉換爲列表{對象,列表{對象}} Linq

namespace Entity 
{ 
    public class MasterMenu 
    { 
     public MasterMenuParent MasterMenuParent; 
     public List<MasterMenuChildOfParent> MasterMenuChildOfParent; 
    } 

    public class MasterMenuParent 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
    } 

    public class MasterMenuChildOfParent 
    { 
     public string Id { get; set; } 
     public string Name { get; set; } 
     public string ParentId { get; set; } //It's a foreign key that link to MasterMenuParent.Id 
    } 
} 

我查詢數據庫中的數據並轉換爲實體。 對於getDataMasterMenu1()我使用兩個循環。 對於getDataMasterMenu2()我使用了一個Loop和linq。 對於getDataMasterMenu3()我只想使用linq,但我不知道該怎麼做,這有可能嗎?

using Entity; 
public partial class Test : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     List<MasterMenu> list1 = getDataMasterMenu2(); 
     List<MasterMenu> list2 = getDataMasterMenu2(); 
     List<MasterMenu> list3 = getDataMasterMenu3(); 
    } 

    //Using two Loop 
    private List<MasterMenu> getDataMasterMenu1() 
    { 
     List<MasterMenu> result = new List<MasterMenu>(); 
     List<MasterMenuChildOfParent> tempMasterMenuChildOfParent = new List<MasterMenuChildOfParent>(); 
     DataTable dtMasterMenuParent = new DataTable(); //Assume: I already query data from database and then load to datatable that has field: ID, NAME 
     DataTable dtMasterMenuChild = new DataTable(); //Assume: I already query data from database and then load to datatable that has field: ID, NAME, PARENT_ID 
     for (int i1 = 0; i1 < dtMasterMenuParent.Rows.Count; i1++) 
     { 
      //Select Child Of Parent 
      DataTable dtMasterMenuChildOfParent = (from DataRow dr in dtMasterMenuChild.Rows where dr["PARENT_ID"].Equals(dtMasterMenuParent.Rows[i1]["ID"]) select dr).CopyToDataTable(); 
      for (int i2 = 0; i2 < dtMasterMenuChildOfParent.Rows.Count; i2++) 
      { 
       tempMasterMenuChildOfParent.Add(new MasterMenuChildOfParent 
       { 
        Id = dtMasterMenuChildOfParent.Rows[i2].Field<string>("ID"), 
        Name = dtMasterMenuChildOfParent.Rows[i2].Field<string>("NAME"), 
        ParentId = dtMasterMenuChildOfParent.Rows[i2].Field<string>("PARENT_ID"), 
       }); 
      } 

      result.Add(new MasterMenu 
      { 
       MasterMenuParent = (new MasterMenuParent 
       { 
        Id = dtMasterMenuParent.Rows[i1].Field<string>("ID"), 
        Name = dtMasterMenuParent.Rows[i1].Field<string>("NAME") 
       }), 
       MasterMenuChildOfParent = tempMasterMenuChildOfParent 
      }); 
     } 
     return result; 
    } 

    //Using one Loop and linq 
    private List<MasterMenu> getDataMasterMenu2() 
    { 
     List<MasterMenu> result = new List<MasterMenu>(); 
     DataTable dtMasterMenuParent = new DataTable(); //Assume: I already query data from database and then load to datatable that has field: ID, NAME 
     DataTable dtMasterMenuChild = new DataTable(); //Assume: I already query data from database and then load to datatable that has field: ID, NAME, PARENT_ID 
     for (int i1 = 0; i1 < dtMasterMenuParent.Rows.Count; i1++) 
     { 
      //Select Child Of Parent 
      DataTable dtMasterMenuChildOfParent = (from DataRow dr in dtMasterMenuChild.Rows where dr["PARENT_ID"].Equals(dtMasterMenuParent.Rows[i1]["ID"]) select dr).CopyToDataTable(); 
      result.Add(new MasterMenu 
      { 
       MasterMenuParent = (new MasterMenuParent 
       { 
        Id = dtMasterMenuParent.Rows[i1].Field<string>("ID"), 
        Name = dtMasterMenuParent.Rows[i1].Field<string>("NAME") 
       }), 
       MasterMenuChildOfParent = dtMenuChildOfParent.AsEnumerable().Select(row => 
       new MasterMenuChildOfParent 
       { 
        Id = row.Field<string>("ID"), 
        Name = row.Field<string>("NAME"), 
        ParentId = row.Field<string>("PARENT_ID") 
       }).ToList() 
      }); 
     } 
     return result; 
    } 

    //Using linq 
    private List<MasterMenu> getDataMasterMenu3() 
    { 
     List<MasterMenu> result = new List<MasterMenu>(); 
     DataTable dtMasterMenuParent = new DataTable(); //Assume: I already query data from database and then load to datatable that has field: ID, NAME 
     DataTable dtMasterMenuChild = new DataTable(); //Assume: I already query data from database and then load to datatable that has field: ID, NAME, PARENT_ID 

     //How can I Convert DataTable to List{Object, List{Object}} by Linq 

     return result; 
    } 
} 
+0

你爲什麼不使用這個的ORM?所有這些代碼都可以用singel'dbContext.MasterMenu.Include(m => m.MasterMenuChildOfParent).ToList()'代替。通過使用DataTables,您也​​使用了兩倍的內存。 –

+0

謝謝Mr.Panagiotis Kanavos。我將學習ORM。 – akkapolk

+0

子表中有一個額外的列。所以如果你克隆父表並添加ParentID列,你可以用一個linq來完成。 – jdweng

回答

3

所有你需要做的就是你的窩LINQ:

private List<MasterMenu> getDataMasterMenu3() { 
    var dtMasterMenuParent = new DataTable(); 
    var dtMasterMenuChild = new DataTable(); 

    var result = (from p in dtMasterMenuParent.AsEnumerable() 
      join c in dtMasterMenuChild.AsEnumerable() on p.Field<string>("ID") equals c.Field<string>("PARENT_ID") into cj 
      select new MasterMenu { 
       MasterMenuParent = new MasterMenuParent { Id = p.Field<string>("ID"), Name = p.Field<string>("NAME") }, 
       MasterMenuChildOfParent = cj.Select(c => new MasterMenuChildOfParent { 
        Id = c.Field<string>("ID"), 
        Name = c.Field<string>("NAME"), 
        ParentId = c.Field<string>("PARENT_ID") 
       }).ToList() 
      }).ToList(); 

    return result; 
} 
+0

這是我的預期。非常感謝你。 – akkapolk

相關問題