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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:39:55+00:00 2026-05-20T05:39:55+00:00

This can be done in a million ways … Problem. I have a list

  • 0

This can be done in a million ways …

Problem.
I have a list of servers which belongs to a country.

Countries have many servers, so its a 2 way navigation( I think its called )

Now I have a list of servers with there country set and want to group by country so I can get the opposite list.

List of countries with all there servers attached to the ICollection of the Country ( Servers )

var groupBy = list.GroupBy(x => x.Country, x => x, (c, s) => new {c, s});
foreach (var country in groupBy)
{
}

This will almost give me the result I want, but now I have 2 anon types.
C which is country and S which is the list of servers.

How can I attach the list of servers to the country object’s Collection.

Classes look like this… simplified.

public class Server
{
    public short SID { get; set; }
    public Country Country { get; set; }
}

public class Country
{
    public byte CID { get; set; }
    public ICollection<Server> Servers { get; set; }
}

I guess one could create a new Country with the information and then also put the list of servers in the constructor … but that’s kind of overkill I think.

Well … this odd to be simple but I’m lost here.

Update

I need to select all servers that satisfy some condition. That’s why I have a list of servers with the country. Now I just need to invert the list. Can this be done the other way around ?

I’m using the Repository Pattern with EF 4 Code First.
IRepository
I only have the following methods available with a option to eager load a property:
Single, SingleOrDefault, Find and FindAll

There are no lazy load, they all return ICollection or a single T.

  • 1 1 Answer
  • 3 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-20T05:39:55+00:00Added an answer on May 20, 2026 at 5:39 am

    It sounds like you really just want to build up a Lookup from country to servers:

    var lookup = servers.ToLookup(server => server.Country);
    

    If that’s not what you’re looking for, please let me know how it doesn’t do what you want 🙂

    Admittedly there’s no guarantee that each Country will have all the servers in the original list, and that sounds like it’s that distinction which is really the problem.

    If your Server already has the Country set, can you not assume that the data is already consistent? If not, shouldn’t your Server really have a CID instead of a Country reference? Having a property which will sometimes be consistent with “this” object and sometimes not (i.e. whether server.Country.Contains(server) is guaranteed to return true or not) is ugly IMO.

    EDIT: Okay, a possibly nicer alternative is:

    var query = servers.GroupBy(server => server.Country.CID);
                       .Select(g => new Country { CID = g.Key,
                                                  Servers = g.ToList() });
    

    This is almost the same as fermaref’s approach, but I’m explicitly using Country.CID to avoid two servers with equivalent countries not ending up in the same bucket due to potentially odd equality rules.

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

Sidebar

Related Questions

I know this can be done and i have seen it done using some
I don't think this can be done cleanly, but I'll ask anyway. I have
I have done this a million time and every time has worked, but I
I understand that this can be done at release using the maven-gpg-plugin. However, I
I am wondering if this can be done easily. I am doing some paypal
Simple question. Just wondering if this can be done without me having to enforce
This can certainly be done using a simple script and reading the database. I
I know that this can be easily done by using if(i%5 == 0 OR
I'm basically work with Middleman 2 though if this can only be done in
Can this be done by setting a property? I'd prefer that approach then to

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.