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場所(この場合) を提供し、値はそれらの 3 つのレコードを含む です。KeyKeyMainHallList

しかし、私はそれからも分離したいと思いComponentCodesます。したがって、グループ化Keyは問題ありませんが (私は推測します)、 ComponentCodesMainLevelWest_LevelComponentCodes を同じリストにグループ化したくありません。それらは別のリストに配置する必要があります。

現在の辞書

(辞書のキー == データベース列のキー)

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クエリを調整して、そのような結果を得るにはどうすればよいですか?

4

2 に答える 2

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());
于 2013-07-30T10:32:29.703 に答える