Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用GroupBy的Linq查询_C#_Linq - Fatal编程技术网

C# 使用GroupBy的Linq查询

C# 使用GroupBy的Linq查询,c#,linq,C#,Linq,我正在编写一段执行Linq查询的代码,如下所示: return uow.GetRepository<SomeProjection>().GroupBy(x => x.Key).ToDictionary(x => x.Key, y => y.Select(z => new EditableSomeProjection { Type =

我正在编写一段执行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());
基本上,Linq查询为我提供了一个
字典
,其中字典的
是记录的
键(
MainHall
,在本例中),值是一个
列表
,其中包含这三条记录

但是我想把
组件codes
也从中分离出来。因此,按
分组很好(我猜),但我不希望将
main级别
West\u级别
组件代码也分组在同一个列表中。它们应该放在一个单独的列表中

当前词典

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

我需要什么

(有两个词条的词典)


如何调整此Linq查询以获得这样的结果?

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

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()));
返回
uow.GetRepository()
.GroupBy(x=>x.Key)
.ToDictionary(g1=>g1.Key,
g1=>
g1.GroupBy(y=>y.ComponentCode)
.ToDictionary(g2=>g2.Key,
g2=>
g2.选择(z=>new EditableSomeProjection
{
类型=z.类型,
键=z键,
值=z.值,
Component=z.ComponentCode,
Culture=z.CultureCode,
Code=z.Code.Key,
版本=z.版本,
类别=z.类别
}).ToList());
此代码返回一个字典字典,即:

Dictionary<string,Dictionary<string,List<EditableSomeProjection>>>
字典

其中,外部字典键是
SomeProjection
key
属性,而内部键是
ComponentCode

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

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());
公共类组密钥:IEqualityComparer{
公共字符串组件代码{get;set;}
公共字符串密钥{get;set;}
布尔IEqualityComparer.Equals(对象x,对象y)
{
GroupKey gkx=x作为GroupKey;
GroupKey gky=y作为GroupKey;
返回(gkx==null | | gky==null)?false:gkx.ComponentCode==gky.ComponentCode&&gkx.Key==gky.Key;
}
int IEqualityComparer.GetHashCode(对象obj)
{
返回obj==null?0:obj.GetHashCode();
}
}
//.....
//.....
返回uow.GetRepository().GroupBy(x=>newgroupkey{ComponentCode=x.ComponentCode,Key=x.Key})
y、 选择(z=>new EditableSomeProjection
{
类型=z.类型,
键=z键,
值=z.值,
Component=z.ComponentCode,
Culture=z.CultureCode,
Code=z.Code.Key,
版本=z.版本,
类别=z.类别
}).ToList());

以“但我想分开”开头的段落没有意义。什么是“它”?您不想要什么(包含在结果字典的键中或数据中)?也许可以扩展示例数据以显示您当前拥有的内容,然后显示您想要的内容,并且在谈论您的名称(组件代码等)与数据结构的名称(例如键)时保持一致。@Richard稍微澄清了一下。希望现在更好。您是否尝试:*.GroupBy(x=>new{x.Key,x.ComponentCode})**从您的示例中,我觉得这是两本字典,没有一个字典有单独的条目…您需要的是一个有两个元素的
字典
,它们具有相同的键?这怎么可能?为了使用
GroupKey
类作为
GroupBy
中的键,您需要重写
Equals
GetHashCode
方法,或者提供有效的
IEqualityComparer
。@digEmAll谢谢,那样做有点复杂。我将删除这个答案。关于你这里的问题[.你可以更新你的
.GroupBy
来使用匿名类型,如下所示
.GroupBy(x=>new{x.ComponentCode,x.Key})
。这将不需要你的
IEqualityComparer
@KingKing:实际上是你在GroupBy中使用的类(或通常作为字典键)不需要实现IEqualityComparer;有两种方法可以使其工作:1)重写object.GetHashCode和object.Equals,等于类2)创建另一个实现IEqualityComparer的类,并将其传递给GroupBy的第二个重载。否则,您可以使用NinjaNye所说的匿名对象。。。
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>>>
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());