Click here to Skip to main content
11,925,896 members (61,142 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# LINQ collections
I'm new to using LINQ. Basically I have a Employee Collection Object, which contains a number of employees. Each employee then has a Contact Collection as each employee could have a number of contact numbers/email. The link between the two is a ContactID. How would I go about using LINQ to retrieve some data from the Employee Collection and the rest of the data from the Contact Collection.
Not sure if how I have explains that will make sense to anybody else, but any feedback would be great.
Posted 15-Apr-13 7:23am
Sergey Alexandrovich Kryukov 15-Apr-13 14:39pm
What is the problem? Duplicates? Something else?
BillWoodruff 17-Apr-13 4:00am
Are you saying each item in the Contact Collection within every instance of the Employee Class: is a ContactID ?

What exactly is the form, structure, type, etc. of the ContactID ?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is an attempt to code your scenario.

class Program
       static void Main(string[] args)
           IEnumerable<Employee> empCollection = null;
           // populate the employee collection
           // now if you want to get primary contact of each employee

           var result = from e in empCollection
                        select new
                            EmployeeName = e.Name,
                            PrimaryContact = e.Contacts.Select(c => c.IsPrimaryContact).First()
           // result will be the IEnumerable<anonymoustype>, which contains EmployeeName and the associated primary contacts
   public class Employee
       public string Name { get; set; }
       public IEnumerable<Contact> Contacts { get; set; }
   public class Contact
       public int ContactID { get; set; }
       public string EmailId { get; set; }
       public string Telephone { get; set; }
       public bool IsPrimaryContact { get; set; }

Hope it helps Smile | :)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100