0

我正在编写一段代码来执行如下所示的 Linq 查询:

return uow.GetRepository<SomeProjection>().GroupBy(x => x.Key).ToDictionary(x => x.Key, y =>
                    y.Select(z => new EditableSomeProjection
                    {
                        Type = z.Type,
                        Key = z.Key,
                        Value = z.Value,
                        Component = z.ComponentCode,
                        Culture = z.CultureCode,
                        Code = z.Code.Key,
                        Version = z.Version,
                        Category = z.Category
                    }).ToList());

基本上这个查询工作正常,但我需要调整它,所以它给我一个稍微不同的结果。

这是我所处的场景。我有以下数据:

ComponentCode    CultureCode    Key           Value           .....
MainLevel        fr             MainHall      (fr)Main hall
West_Level       en             MainHall      Entrance
MainLevel        en             MainHall      Main hall

基本上,Linq 查询给了我一个字典的位置是Dictionary记录的位置(在这种情况下),值是其中的三个记录。KeyKeyMainHallList

但我也想把ComponentCodes它和它分开。所以分组Key很好(我猜),但我不希望MainLevelWest_LevelComponentCodes 也被分组在同一个列表中。它们应该放在一个单独的列表中。

当前词典

(字典的键 == 数据库列的键)

Dictionary("MainHall", List(MainLevel        fr             MainHall      (fr)Main hall
                            West_Level       en             MainHall      Entrance
                            MainLevel        en             MainHall      Main hall ));

我需要的

(Dictionary with 2 entries)

Dictionary("MainHall", List(West_Level       en             MainHall      Entrance ));

Dictionary("MainHall", List(MainLevel        fr             MainHall      (fr)Main hall
                            MainLevel        en             MainHall      Main hall ));

How can i adjust this Linq query to give me such a result?

4

2 回答 2

0

You could group-by again the inner list :

return 
uow.GetRepository<SomeProjection>()
   .GroupBy(x => x.Key)
   .ToDictionary(g1 => g1.Key, 
                 g1 =>
                 g1.GroupBy(y => y.ComponentCode)
                   .ToDictionary(g2 => g2.Key,
                                 g2 =>
                                 g2.Select(z => new EditableSomeProjection
                                 {
                                    Type = z.Type,
                                    Key = z.Key,
                                    Value = z.Value,
                                    Component = z.ComponentCode,
                                    Culture = z.CultureCode,
                                    Code = z.Code.Key,
                                    Version = z.Version,
                                    Category = z.Category
                                 }).ToList()));

This code returns a dictionary of dictionaries i.e. :

Dictionary<string,Dictionary<string,List<EditableSomeProjection>>>

where the outer dictionary key is the Key property of SomeProjection and the inner key is the ComponentCode

于 2013-07-30T09:35:57.273 回答
0

I think this works, however defining a class for the group key is a little complicated, but the code may looks clearer.

public class GroupKey : IEqualityComparer {
  public string ComponentCode {get;set;}
  public string Key {get;set;}
  bool IEqualityComparer.Equals(object x, object y)
  {
     GroupKey gkx = x as GroupKey;
     GroupKey gky = y as GroupKey;
     return (gkx == null || gky == null) ? false : gkx.ComponentCode == gky.ComponentCode && gkx.Key == gky.Key;
  }
  int IEqualityComparer.GetHashCode(object obj)
  {
     return obj == null ? 0 : obj.GetHashCode();
  }
}
//.....
//.....
return uow.GetRepository<SomeProjection>().GroupBy(x => new GroupKey{ComponentCode=x.ComponentCode, Key=x.Key}).ToDictionary(x => x.Key, y =>
                y.Select(z => new EditableSomeProjection
                {
                    Type = z.Type,
                    Key = z.Key,
                    Value = z.Value,
                    Component = z.ComponentCode,
                    Culture = z.CultureCode,
                    Code = z.Code.Key,
                    Version = z.Version,
                    Category = z.Category
                }).ToList());
于 2013-07-30T10:32:29.703 回答