Click here to Skip to main content
15,883,887 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
How do I change this SQL insert code to LINQ?

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



linqtosql entity framework sorry did not mention earlier.
Posted
Updated 12-Mar-12 4:14am
v2
Comments
Dean Oliver 12-Mar-12 10:06am    
is this linqtosql or entity framework you want to use in your project?
postonoh 12-Mar-12 10:12am    
Yes! sorrow I did not mention this.
Dean Oliver 12-Mar-12 10:13am    
so which is it?
postonoh 12-Mar-12 10:31am    
I created a custom role provider for forms security. I have the following three tables. User, User_In_Role, and Roles. I need for when a user register. It places the into the default group of new user. I submitted this question I believe I had the fix but did not work. I believe with the aspnetdb.net security sproc this is the key to making sure the new user set properly.

My original question is here:
http://www.codeproject.com/Questions/343705/How-set-a-default-role-with-custom-RoleProvider
Dean Oliver 13-Mar-12 1:11am    
Please place this comment in your question. And linqtosql and entity framework are two different technologies.

You can change SQL to Linq. Use this tool:

http://www.sqltolinq.com/[^]
 
Share this answer
 
v2
C#
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900