gpt4 book ai didi

c# - 如何在 nHibernate 中部分投影具有多个字段的子对象

转载 作者:行者123 更新时间:2023-11-30 20:46:28 24 4
gpt4 key购买 nike

我有以下 nHibernate 查询,它根据类(class) ID 选择一门类(class),然后在初始提取时返回类(class)对象的选定字段,查询执行时没有任何问题。

MatchMode option = ... 
CourseItem courseAlias = null;
TeacherItem teacherAlias = null;

var query = session.QueryOver<CourseItem>()
.JoinAlias(c => c.Teacher, () => teacherAlias)
.Where(c => c.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(c => c.CourseID).WithAlias(() => courseAlias.CourseID)
.Select(c => c.IsActive).WithAlias(() => courseAlias.IsActive)
.Select(c => c.CourseDesc).WithAlias(() => courseAlias.CourseDesc)
.Select(c => c.Teacher).WithAlias(() => courseAlias.Teacher))
.TransformUsing(Transformers.AliasToBean<CourseItem>())
.List<CourseItem>();

我想更进一步地查询只返回部分教师对象,假设我只想返回 ID 和姓名。因此,我将预计列表更新为如下:

var query = session.QueryOver<CourseItem>()
.JoinAlias(c => c.Teacher, () => teacherAlias)
.Where(c => c.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(c => c.CourseID).WithAlias(() => courseAlias.CourseID)
.Select(c => c.IsActive).WithAlias(() => courseAlias.IsActive)
.Select(c => c.CourseDesc).WithAlias(() => courseAlias.CourseDesc)
.Select(c => c.Teacher.ID).WithAlias(() => courseAlias.Teacher.ID)
.Select(c => c.Teacher.Name).WithAlias(() => courseAlias.Teacher.Name))
.TransformUsing(Transformers.AliasToBean<CourseItem>())
.List<CourseItem>();

查询不起作用,因为 nHibernate 不知道如何根据 Teacher.ID 和 Teacher.Name 进行解析。关于是否可以不将整个子对象取回父对象有什么想法吗?

我已经尝试了以下查询并且它有效这不是我完全想要的结果

var query = session.QueryOver<CourseItem>(() => courseAlias)
.JoinAlias(() => courseAlias.Teacher, () => teacherAlias)
.Where(() => courseAlias.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(() => courseAlias.CourseID)
.Select(() => courseAlias.IsActive)
.Select(() => courseAlias.CourseDesc)
.Select(() => teacher.ID)
.Select(() => teacher.Name))
.List<object[]>();

我可以查询正确的值,但无法将其正确转换回类(class)/教师数据类型。

有什么想法吗?

谢谢!

最佳答案

我们确实可以使用自定义转换器。有一个,我正在使用它进行非常非常深的投影(包括动态对象 - 5.1.13. component, dynamic-component)

接受它(如果需要调整它),你的最终查询可能是这样的

// just the last lines are different
var query = session.QueryOver<CourseItem>()
.JoinAlias(c => c.Teacher, () => teacherAlias)
.Where(c => c.CourseID.IsInsensitiveLike(strNumber, option))
.SelectList(list => list
.Select(c => c.CourseID).WithAlias(() => courseAlias.CourseID)
.Select(c => c.IsActive).WithAlias(() => courseAlias.IsActive)
.Select(c => c.CourseDesc).WithAlias(() => courseAlias.CourseDesc)

// the native WitAlias would not work, it uses expression
// to extract just the last property
//.Select(c => c.Teacher.ID).WithAlias(() => courseAlias.Teacher.ID)
//.Select(c => c.Teacher.Name).WithAlias(() => courseAlias.Teacher.Name))

// so we can use this way to pass the deep alias
.Select(Projections.Property(() => teacherAlias.ID).As("Teacher.ID"))
.Select(Projections.Property(() => teacherAlias.Name).As("Teacher.Name"))

// instead of this
// .TransformUsing(Transformers.AliasToBean<CourseItem>())
// use this
.TransformUsing(new DeepTransformer<CourseItem>())

如果您的别名与属性名称匹配,转换器将构建对象树...

关于c# - 如何在 nHibernate 中部分投影具有多个字段的子对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26893475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com