dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Join of list

  1. #1
    Join Date
    Nov 2019
    Posts
    2

    Join of list

    I have

    public class employee
    {
    public string keyNo {get; set;}
    public string firstName {get; set;}
    public string lastName {get; set;}
    public string division {get; set;}
    }

    And

    List<employee> staff - new List <employee>();


    Then I load the list with data.

    I want to get out a cvs list of keyNo for each division:
    KeyNumbers = join(',' ??????

    Is there a way to use join and Linq and avoid a foreach loop ?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,176

    Re: Join of list

    Following C# style and naming conventions, we have:

    Code:
    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp7
    {
        public class Employee
        {
            public string KeyNo { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Division { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                var staff = new List<Employee>
                {
                    new Employee {
                        KeyNo = "100",
                        FirstName = "John",
                        LastName = "Smith",
                        Division = "Division 1"
    
                    }
                    , new Employee
                    {
                        KeyNo = "200",
                        FirstName = "Bill",
                        LastName = "Johnson",
                        Division = "Division 2"
                    }
                };
    
                var keyNos = string.Join(',', staff.Select(e => e.KeyNo));
    
                Console.WriteLine(keyNos);
            }
        }
    }
    Outputs:
    Code:
    100,200

  3. #3
    Join Date
    Nov 2019
    Posts
    2

    Re: Join of list

    Thanks. Works a treat

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)