本文介绍了如何将其更改为linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将此SQL插入代码更改为LINQ?

How do I change this SQL insert code to LINQ?

INSERT INTO dbo.aspnet_UsersInRoles (UserId, RoleId)
    SELECT UserId, RoleId
    FROM @tbUsers, @tbRoles




linqtosql实体框架抱歉,之前没有提到.




linqtosql entity framework sorry did not mention earlier.

推荐答案


public override void AddUsersToRoles(string[] usernames, string[] roleNames)
         {
              string[] allRoles = GetAllRoles();

              IEnumerable<string> roles = allRoles.Intersect(roleNames);

              if(roles.Any())
              {
                   //RemoveUsersFromRoles(usernames, roleNames);

                   using (var db = new EEGScreeningEntities())
                   {
                        List<int> mlist = (from m in db.aspnet_Users
                                           where usernames.Contains(m.UserName)
                                           select m.UserId).ToList();


                        List<int> glist = (from g in db.aspnet_Roles
                                           where roleNames.Contains(g.RoleName)
                                           select g.RoleId).ToList();


                       var mglist = (from m in mlist from g in glist select new User_In_Roles {UserId = m, RoleId = g }).FirstOrDefault();
                        db.User_In_Roles.AddObject(mglist);

                       db.SaveChanges();

                    }

              }</int></int></string>


有什么可以凝聚的.


is there to condense this.


这篇关于如何将其更改为linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 03:55