在linq中将动态行转换为列

时间:2016-08-02 09:04:29

标签: linq

enter image description here

  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可以包含任何行...请不要帮我获得所需的结果

我想将结果作为上面的屏幕

1 个答案:

答案 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 = new List()创建一个新列表来获得上述结果,如下面的代码

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

希望它对你有用