gpt4 book ai didi

nhibernate queryover join 别名与 where-or 子句

转载 作者:行者123 更新时间:2023-12-04 02:56:22 27 4
gpt4 key购买 nike

我就贴一下代码:

public IList<Dish> GetByNameDishTypes(IEnumerable<string> names,
IEnumerable<string> dishTypes)
{
// return dishes (ie food) whose name is in the names enumerable,
// or whose type is in the dish types enumerables
// (this would be 'breakfast', 'dinner') ..

var namesArr = names != null ? names.ToArray() : new string[0];
var dishTypesArr = dishTypes != null ? dishTypes.ToArray() : new string[0];

var criteria = this.Session.CreateCriteria<Dish>();
criteria.CreateAlias("DishType", "dt");
criteria.Add(
Restrictions.Or
(
Restrictions.In("Name", namesArr),
Restrictions.In("dt.Name", dishTypesArr)
));

return criteria.List<Dish>();

/* get this to work?
Dish d = null;
DishType dt = null;
return this.Session.QueryOver<Dish>(() => d)
.JoinAlias(() => d.DishType, () => dt)
.Where(
Restrictions.Or
(
Restrictions.In(d.Name, namesArr),
Restrictions.In(dt.Name, dishTypesArr)
))
.List<Dish>(); */
}

所以,我想知道如何使用 QueryOver 做到这一点。我发布的这个 QueryOver 代码抛出了一个空引用异常。标准代码有效。

谢谢 :)

最佳答案

有几种方法可以在 QueryOver 中重写它,但最干净的可能是:

Dish d = null;
DishType dt = null;
return this.Session.QueryOver<Dish>(() => d)
.JoinAlias(() => d.DishType, () => dt)
.Where(() => d.Name.IsIn(namesArr) || dt.Name.IsIn(dishTypesArr))
.List<Dish>();

这将生成如下所示的 SQL:

SELECT this_.*
FROM [Dish] this_
inner join [DishType] dt1_
on this_.DishTypeId = dt1_.Id
WHERE (this_.Name in (/* Comma separated list of names */)
or dt1_.Name in (/* Comma separated list of types */))

关于nhibernate queryover join 别名与 where-or 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12030329/

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