Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 593071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:48:23+00:00 2026-05-13T15:48:23+00:00

I have a LINQ TO SQL query which retrieves all the users along with

  • 0

I have a LINQ TO SQL query which retrieves all the users along with their roles:

var userRoles = from u in db.GetTable<User>()
                            join ur in db.GetTable<UserRole>()
                                on u.UserID equals ur.UserID
                            join r in db.GetTable<Role>()
                                on ur.RoleID equals r.RoleID
                                orderby u.UserID
                            select new
                                       {
                                           u.UserID,
                                           r.RoleName
                                       };

A user in the system can have multiple roles. The result of this query (in a table format) looks like:

1 Admin
1 Employee
2 Employee
3 Employee

How can I re-write this query to return all user roles as comma separated values like:

1 Admin, Employee
2 Employee
3 Employee

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:48:23+00:00Added an answer on May 13, 2026 at 3:48 pm

    Hey Kumar, I created a small console app to mimic the data I believe you have. I think it exhibits the behavior you are looking for though. It’s not the greatest code in the world, but I think the algorithm is the point. I just threw together a quick override of ToString() to display the data correctly.

    The main change I did was to create a defined class for the data to be displayed, and break the linq query into two separate pieces:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Test
    {
        class Program
        {
            static void Main()
            {
                var users = new List<User>
                                {
                                    new User
                                        {
                                            UserID = "1"
                                        },
                                    new User
                                        {
                                            UserID = "2"
                                        },
                                    new User
                                        {
                                            UserID = "3"
                                        }
                                };
    
                var roles = new List<Role>
                                {
                                    new Role
                                        {
                                            RoleID = "1",
                                            RoleName = "Admin"
                                        },
                                    new Role
                                        {
                                            RoleID = "2",
                                            RoleName = "Employee"
                                        }
                                };
    
                var userRoles = new List<UserRole>
                                    {
                                        new UserRole
                                            {
                                                UserID = "1",
                                                RoleID = "1"
                                            },
    
                                        new UserRole
                                            {
                                                UserID = "1",
                                                RoleID = "2"
                                            },
    
                                        new UserRole
                                            {
                                                UserID = "2",
                                                RoleID = "2"
                                            },
    
                                        new UserRole
                                            {
                                                UserID = "3",
                                                RoleID = "2"
                                            }
                                    };
    
                var userRoles2 = from u in users
                                 orderby u.UserID
                                 select new UserList
                                 {
                                     UserID = u.UserID,
                                     Roles = (from r in roles
                                                join ur in userRoles
                                                on u.UserID equals ur.UserID
                                                where ur.RoleID == r.RoleID
                                                select r).ToList()
                                 };
    
                foreach (var item in userRoles2)
                {
                    Console.WriteLine(item);
                }
                Console.ReadKey();
            }
        }
    
        public class User
        {
            public string UserID;
        }
    
        public class UserRole
        {
            public string UserID;
            public string RoleID;
        }
    
        public class Role
        {
            public string RoleID;
            public string RoleName;
        }
    
        public class UserList
        {
            public string UserID;
            public List<Role> Roles;
    
            public override string ToString()
            {
                string output = UserID + " ";
                foreach (var role in Roles)
                {
                    output += role.RoleName + ", ";
                }
                output = output.Substring(0, output.Length - 2);
                return output;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.