我有一个查询来获取 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/