开发者

LINQ GroupBy, convert a one-to-one list to a one-to-many

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 th开发者_开发知识库e 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.


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.


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

Linq is for querying and it has a functional mindset where you generate new instances instead of modifying existing instances.

If you want to modify instances, a foreach loop is superior:

List<Server> servers = GetServers();

foreach(Server server in servers)
{
  Country c = server.Country;
  c.Servers.Add(server);
}

If you must use Linq:

List<Server> servers = GetServers();

foreach(IGrouping<Country, Server> g in servers.GroupBy(s => s.Country))
{
  Country c = g.Key;
  c.Servers = g.ToList();
}

Now I just need to invert the list.

ILookup<Country, Server> lookup = servers.ToLookup(s => s.Country);
List<Country> countries = lookup.Select(g => g.Key).ToList();
foreach(Country c in countries)
{
  c.Servers = lookup[c].ToList();
}
return countries;


If you can't guarantee that the Country object in each Server object already contains all the Servers, you can create a new Country object:

var groupBy = list.GroupBy(
                    x => x.Country, 
                    x => x, (c, s) => 
                                new Country 
                                {
                                  CID = c.CID, 
                                  Servers =  s.ToList()});

However, I'm not sure if that's what you want.

If you can guarantee that the Country object will always contain all Servers, you can could use the following:

var countries = list.SelectMany(s => s.Country).Distinct();

or, if your Country doesn't implement IEquateable:

var groupBy = list.GroupBy(x => x.Country).Select(x => x.Key);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜