2016-08-02 71 views
0

enter image description here轉換動態行成列在LINQ

var Getitems = (from u in db.EmailSettings 
         join s in db.SecurityRoles on u.SecurityRoleID equals s.SecurityRoleID 
         group u by new { u.SecurityRoleID,s.SecurityRoleName} into g 
         select new 
         { 
          TransTypeId = (from v in db.EmailSettings 
              //join s in db.SecurityRoles on v.SecurityRoleID equals s.SecurityRoleID 
              where v.SecurityRoleID == g.Key.SecurityRoleID 

              select new 
              { 
               //username = s.SecurityRoleName, 
               trans = v.TransTypeID 
              }), 
            SecurityRoleName=g.Key.SecurityRoleName, 
            SecurityRoleId=g.Key.SecurityRoleID, 


         }).ToList(); 

例如在TransTypeId我有3行數據..我想TAT轉換成單排像SecurityRoleName,SecurityRoleId,ROW1,ROW2,ROW3 .. 其中TransTypeId可以有任何的行號。做任何一個幫助我得到期望的結果

我想要的結果爲上述畫面

+0

這不是很清楚你想達到什麼目的?結果列表元素的**類型**是什麼? –

+0

伴侶你想從2個表'EmailSettings'和'TransTypeId'中獲取數據,以獲得一個'複雜對象'。因此,發佈複雜對象的2表模型和模型類。然後我們可以想出辦法來幫助你。 –

+0

我把它放在一個新的對象中......並且感謝所有人 –

回答

0
var Getitems = (from u in db.EmailSettings 
        join s in db.SecurityRoles on u.SecurityRoleID equals s.SecurityRoleID 
        group u by new { u.SecurityRoleID,s.SecurityRoleName} into g 
        select new 
        { 
         TransTypeId = (from v in db.EmailSettings 
             //join s in db.SecurityRoles on v.SecurityRoleID equals s.SecurityRoleID 
             where v.SecurityRoleID == g.Key.SecurityRoleID 

             select new 
             { 
              //username = s.SecurityRoleName, 
              trans = v.TransTypeID 
             }), 
           SecurityRoleName=g.Key.SecurityRoleName, 
           SecurityRoleId=g.Key.SecurityRoleID, 


        }).ToList(); 

一壓腳提升,你可以通過在每一個每個循環創建一個新的列表使用transactiontypenames =新名單(),如下面的代碼

List<transactiontypename> transactiontypenames; 
     transactiontypename transactiontypenameobj; 
     foreach (var x in Getitems) 
     { 
      transactiontypenames = new List<transactiontypename>(); 
       objsecurityVM = new SecurityRoleVM(); 
       objsecurityVM.User = x.SecurityRoleName; 
       objsecurityVM.SecurityRoleId = x.SecurityRoleId; 

       foreach (var y in x.TransTypeId) 
       { 
        transactiontypenameobj = new transactiontypename(); 
        transactiontypenameobj.Transtypes = y.trans; 
        transactiontypenames.Add(transactiontypenameobj); 
       } 
       objsecurityVM.TransTypes = transactiontypenames; 

       objlistsecurityroleVM.Add(objsecurityVM); 

      } 

     objEmailSetting.TableData = objlistsecurityroleVM; 
     return View(objEmailSetting); 

希望這將是對您有用得到上述結果