c# - nhibernate 投影到匿名类型

标签 c# nhibernate group-by anonymous-types queryover

我有一个查询来获取 Child 的计数Parent 的对象对象。
我需要将结果转换为 List<KeyValuePair<int, int>>
想不通。

Child childAlias = null;
Parent parentAlias = null;
int[] parentIds = new int[]{1,2,3};

var temp = sess.QueryOver<Parent>()
   .JoinQueryOver(p => p.Children, () => childAlias)
   .Where(c => c.Parent.Id.IsIn(parentIds))
   .Select(Projections.ProjectionList()
      .Add(Projections.GroupProperty(Projections.Property<Parent>(x => x.Id)))
      .Add(Projections.Count(() => childAlias.Id)))
  .List<object[]>();

我需要这个List<object[]>成为List<KeyValuePair<int, int>>
我知道它涉及Select使用匿名对象但无法弄清楚

最佳答案

工作查询应如下所示:

Child childAlias = null;
Parent parentAlias = null;
int[] parentIds = new int[] {1, 2, 3};

var temp = sess.QueryOver<Parent>()
    .JoinQueryOver(p => p.Children, () => childAlias)
    .WhereRestrictionOn(c => c.Parent.ID).IsIn(parentIds)
    .Select(Projections.ProjectionList()
        .Add(Projections.GroupProperty(Projections.Property<Parent>(x => x.ID)))
        .Add(Projections.Count(() => childAlias.ID)))
    .List<object[]>()
    .Select(x => new KeyValuePair<int,int>((int)x[0], (int)x[1]));

关于c# - nhibernate 投影到匿名类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33834143/

相关文章:

hibernate - 如果我学习 NHibernate,我会知道 Hibernate 吗? Spring.net 和 (Java) Spring 怎么样?

c# - NHibernate 除了存储过程什么都没有

sql - 对一列进行求和并从其他列中随机选择数据的算法

mysql - GROUP BY字段的顺序影响MySQL查询结果

c# - 使用 ASP.NET 访问 Active Directory

C# Codility 问题 number of caSTLes 如何解决?

C#: 'listView_ItemDrag' 没有重载匹配委托(delegate) 'System_Eventhandler'

c# - NHibernate 使用 WCF 序列化延迟加载的实体

mysql - 在使用 GROUP BY 之前对数据进行排序?

c# - 在 C# 中将对象列表转换为其接口(interface)类型