用GroupBy查询Linq

时间:2013-07-30 09:28:40

标签: c# linq

我正在编写一段执行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,其中字典的Key是记录的Key(在这种情况下为MainHall),值为a List包含其中的三个记录。

但是我想将ComponentCodes与它分开。因此Key的分组很好(我猜),但我不希望将MainLevelWest_Level ComponentCodes分组到同一个列表中。它们应该放在一个单独的列表中。

当前词典

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

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

我需要什么

(带2个条目的词典)

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

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

如何调整此Linq查询以获得此类结果?

2 个答案:

答案 0 :(得分:0)

您可以再次分组内部列表:

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()));

此代码返回词典字典,即:

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

其中外部词典键是Key的{​​{1}}属性,内键是SomeProjection

答案 1 :(得分:0)

我认为这是有效的,但是为组密钥定义一个类有点复杂,但代码可能看起来更清晰。

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());