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

  • Home
  • SEARCH
  • 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 9195131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:33:47+00:00 2026-06-17T21:33:47+00:00

class Program { static void Main(string[] args) { List<EmployeeLocalRegister> lclEmployees = new List<EmployeeLocalRegister>() {

  • 0
class Program
{
    static void Main(string[] args)
    {
        List<EmployeeLocalRegister> lclEmployees = new List<EmployeeLocalRegister>() { 
            new EmployeeLocalRegister(){Name = "A", Phone= "A1"}, 
            new EmployeeLocalRegister(){Name = "B", Phone= "B1"},
            new EmployeeLocalRegister(){Name = "A", Phone= "A2"}, 
            new EmployeeLocalRegister(){Name = "B", Phone= "B2"},
            new EmployeeLocalRegister(){Name = "B", Phone= "B3"},
            new EmployeeLocalRegister(){Name = "C", Phone= "C1"}};

        List<EmployeeTelDir> telDir = new List<EmployeeTelDir>();

        var queryEmployeeLocalRegisterByName =
        from empl in lclEmployees
        group empl by empl.Name;

        foreach (var employeeGroup in queryEmployeeLocalRegisterByName)
        {
            Console.WriteLine(employeeGroup.Key);
            List<string> phone = new List<string>();
            foreach (EmployeeLocalRegister employee in employeeGroup)
            {

                Console.WriteLine("    {0}", employee.Phone);
                phone.Add(employee.Phone);
            }

            telDir.Add(new EmployeeTelDir() { Name = employeeGroup.Key, Phone = phone });
        }

        Console.ReadKey();
    }
}

public class EmployeeLocalRegister
{
    public string Name;
    public string Phone;
}

public class EmployeeTelDir
{
    public string Name;
    public List<string> Phone;
}

}

I am using above code to convert a List<EmployeeLocalRegister> to List<EmployeeTelDir>. Is this the only optimized way?

Can I write simpler code for the back and forth conversion from List<EmployeeLocalRegister> to List<EmployeeTelDir> and vice-versa using Linq query?

  • 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-06-17T21:33:48+00:00Added an answer on June 17, 2026 at 9:33 pm

    If you don’t need Console.WriteLine(...) your code can be summarized with LINQ :

    List<EmployeeTelDir> telDir = (from empl in lclEmployees
                                   group empl by empl.Name into employeeGroup
                                   select new EmployeeTelDir
                                   {
                                       Name = employeeGroup.Key,
                                       Phone = (from employee in employeeGroup
                                               select employee.Phone).ToList()  // The ToList() is the Holy Grail of the LINQ queries
                                   }).ToList();
    

    For the inverted operation:

    List<EmployeeLocalRegister> inverse = (from employeeTelDir in telDir
                                           from phone in employeeTelDir.Phone  // Doing 2 from ... in ... successively corresponds to the SelectMany() LINQ method
                                           select new EmployeeLocalRegister
                                           {
                                               Name = employeeTelDir.Name,
                                               Phone = phone
                                           }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
class Program { static void Main(string[] args) { List<A> myList = new List<A> {new
I have the following code: class Program { static void Main(string[] args) { new
Why is my list not returning anything? class Program { static void Main(string[] args)
I have the following code class Program { static void Main(string[] args) { List<A>
class Program { static void Main(string[] args) { string value = "12345"; Type enumType
I have below code: class Program { static void Main(string[] args) { Task[] tasks
i have written the following code class Program { static void Main(string[] args) {
I am trying to do the following: class Program { static void Main(string[] args)
First look at this code: class Program { static void Main(string[] args) { var

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.