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 7957975
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:20:52+00:00 2026-06-04T04:20:52+00:00

I am using Entity Framework and perform a query on a database that returns

  • 0

I am using Entity Framework and perform a query on a database that returns a company which in turn has many contacts per company.

I have the two scenarios where i want to group the MyContacts which have the same first and last name.

Whilst i can loop through an array of new objects where the result is stored, I am using Entity Framework and it will be slow to have to load data more than once, so if possible I would prefer to maintain the objects as-is in a result set.

The plan is i can loop through the results array, make a change to the MyContacts object, and update the object into the EF repository.

The first scenario is grouping a contact list by name, but i am unsure on how to group without making a new class dataset.

The second scenario is more complex, i have a list of MyAccounts (each of which has a list of MyContacts), i would like to return the MyContacts for all the list, grouped by first and lastname, with the original classes returned if possible.

Many thanks, Chris.

I have removed data access and made a simple example below:

class MyAccount
{
    public string accountName { get; set; }
    public List<MyContact> Contacts { get; set; }
}

class MyContact
{
    public string firstname { get; set; }
    public string lastname  { get; set; }
}

MyContact contactA = new MyContact() { firstname = "Chris", lastname = "b", ID = 100 };
MyContact contactB = new MyContact() { firstname = "Chris", lastname = "b", ID = 101 };
MyContact contactC = new MyContact() { firstname = "Peter", lastname = "Bread", ID = 102 };
MyContact contactD = new MyContact() { firstname = "James", lastname = "apple", ID = 103 };
MyContact contactE = new MyContact() { firstname = "Richard", lastname = "Brian", ID = 104 };
MyContact contactF = new MyContact() { firstname = "James", lastname = "apple", ID = 105 };

List<MyContact> contacts = new List<MyContact>();
contacts.AddRange(new MyContact[] { contactA, contactB, contactC, contactD, contactE, contactF } );
// how do i get a list of items, grouped by same first and lastname?

MyAccount companyA = new MyAccount() { accountName = "CompanyA", Contacts = new List<MyContact>() };
companyA.Contacts.AddRange(new MyContact[] { contactA, contactB, contactC });
MyAccount companyB = new MyAccount() { accountName = "CompanyB", Contacts = new List<MyContact>() };
companyB.Contacts.AddRange(new MyContact[] { contactA, contactB, contactC });
MyAccount companyC = new MyAccount() { accountName = "CompanyB", Contacts = new List<MyContact>() };
companyB.Contacts.AddRange(new MyContact[] { contactA, contactB, contactC, contactD, contactE });
List<MyAccount> companyList = new List<MyAccount>(new MyAccount[] { companyA, companyB, companyC });
// from the companyList, is there any way to get a list of MyContact types grouped by duplicate first and lastname?
  • 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-04T04:20:53+00:00Added an answer on June 4, 2026 at 4:20 am

    Try this…

        var result = from c in contacts
                     group c by new { c.firstname, c.lastname } into g
                     select g.ToList();
    
        var result1 = from c in companyList.SelectMany(company => company.Contacts)
                      group c by new { c.firstname, c.lastname } into g
                      select g.ToList();
    

    Now you get IEnumerable of Lists.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into a situation using the Entity Framework (EF) that has me totally
I am using Entity Framework 4.1 to perform CRUD operations against my database. I
I'm using Entity Framework in my project, and I have the problem that, once
Let me preface this question and state that using Entity Framework is not an
I am using Entity Framework. One of my Entities (MyEntity) has a dependency on
When using the Entity Framework, does ESQL perform better than Linq to Entities? I'd
using Entity Framework 4.1 is it possible to perform a left outer join with
I am using Entity Framework 4.1 for database access and would like to Unit
I'm running into some issues when constructing a query using LINQ and Entity Framework.
It seems that serializing Entity Framework objects into JSON is not possible using either

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.