gpt4 book ai didi

tsql - 如何使用 NHibernate ICriteria 进行分组、获取关联和 T-SQL 函数

转载 作者:行者123 更新时间:2023-12-05 00:42:05 29 4
gpt4 key购买 nike

我想创建以下 T-SQL 语句:

SELECT  SUM (sa.Amount) as 'SumAmount',
SUM(sa.Cost) as 'SumCost',
gg.[Description] as 'Goodsgroup', Month(sa.[Date]) as 'Month'
FROM SalesmanArticle sa
INNER JOIN Article a
ON a.ArticleId = sa.ArticleId
INNER JOIN GoodsGroup gg
ON gg.GoodsGroupId = a.GoodsGroupId
GROUP BY gg.[Description], Month(sa.[Date])
ORDER BY 'Month', 'Goodsgroup'

NHibernates ICriteria 是否可以做到这一点?

如何使用 Month-T-SQL-Function?

我必须手动加入还是 ICriteria API 知道当我使用 propetyName 'SalesmanArticle.Article.Goodsgroup.Description' 时它必须加入文章和商品组?

编辑:

现在我在这里写了这段代码:
// typesafe properties
string article = typeof(Article).Name;
string goodsGroup = typeof(GoodsGroup).Name;
string salesmanArticle = typeof(SalesmanArticle).Name;

string amount = Reflector.GetPropertyName<SalesmanArticle>(x => x.Amount);
string cost = Reflector.GetPropertyName<SalesmanArticle>(x => x.Cost);
string description = string.Format("{0}.{1}",
goodsGroup, Reflector.GetPropertyName<SalesmanArticle>(x => x.Article.GoodsGroup.Description));
string date = Reflector.GetPropertyName<SalesmanArticle>(x => x.Date);

string formatedDate = string.Format("MONTH([{0}])", date);

return GetSession()
// FROM
.CreateCriteria(typeof(SalesmanArticle), salesmanArticle)
// JOIN
.CreateCriteria(article, article, JoinType.InnerJoin)
.CreateCriteria(goodsGroup, goodsGroup, JoinType.InnerJoin)
// SELECT
.SetProjection(Projections.ProjectionList()
.Add(Projections.Sum(amount))
.Add(Projections.Sum(cost))
// GROUP BY
.Add(Projections.GroupProperty(description))
.Add(Projections.SqlGroupProjection(formatedDate, formatedDate, new[]{"MyDate"} , new[] { NHibernateUtil.Int32 })))
.List();

但是抛出了一个 AdoException:

could not execute query [ SELECT sum(this_.Amount) as y0_, sum(this_.Cost) as y1_, goodsgroup2_.Description as y2_, MONTH([Date]) FROM [SalesmanArticle] this_ inner join [Article] article1_ on this_.ArticleId=article1_.ArticleId inner join [GoodsGroup] goodsgroup2_ on article1_.GoodsGroupId=goodsgroup2_.GoodsGroupId GROUP BY goodsgroup2_.Description, MONTH([Date]) ]

[SQL: SELECT sum(this_.Amount) as y0_, sum(this_.Cost) as y1_, goodsgroup2_.Description as y2_, MONTH([Date]) FROM [SalesmanArticle] this_ inner join [Article] article1_ on this_.ArticleId=article1_.ArticleId inner join [GoodsGroup] goodsgroup2_ on article1_.GoodsGroupId=goodsgroup2_.GoodsGroupId GROUP BY goodsgroup2_.Description, MONTH([Date])]



奇怪的是,NHibernate 尝试创建 2 个查询?!

他们两个都是正确的!

而不是代码行
.Add(Projections.SqlGroupProjection(formatedDate, formatedDate, new[]{"MyDate"} , new[] { NHibernateUtil.Int32 })))

我用了
.Add(Projections.SqlFunction("MONTH", NHibernateUtil.Int32, Projections.GroupProperty(date))))

SqlFunction 的问题在于它创建了一个 GROUP BY sa.Date 而不是 MONTH(sa.Date)。但是这种方法在语法上是正确的。

所以我切换到 SqlGroupProjection 方法。

但无论如何它不起作用。

有谁能够帮助我?

最佳答案

我解决了。这是正确的代码:

public class SalesmanArticleRepository : Repository<SalesmanArticle>, ISalesmanArticleRepository
{
public IList GetAllAll()
{
// typesafe properties
string article = typeof(Article).Name;
string goodsGroup = typeof(GoodsGroup).Name;
string salesmanArticle = typeof(SalesmanArticle).Name;

string amount = Reflector.GetPropertyName<SalesmanArticle>(x => x.Amount);
string cost = Reflector.GetPropertyName<SalesmanArticle>(x => x.Cost);
string description = string.Format("{0}.{1}",
goodsGroup, Reflector.GetPropertyName<SalesmanArticle>(x => x.Article.GoodsGroup.Description));
string date = Reflector.GetPropertyName<SalesmanArticle>(x => x.Date);

string formatedDateSql = string.Format("month({{alias}}.[{0}]) as mydate", date);
string formatedDateGroupBy = string.Format("month({{alias}}.[{0}])", date);

return GetSession()
// FROM
.CreateCriteria(typeof(SalesmanArticle), salesmanArticle)
// JOIN
.CreateCriteria(article, article, JoinType.InnerJoin)
.CreateCriteria(goodsGroup, goodsGroup, JoinType.InnerJoin)
// SELECT
.SetProjection(Projections.ProjectionList()
.Add(Projections.Sum(amount))
.Add(Projections.Sum(cost))
// GROUP BY
.Add(Projections.GroupProperty(description))
.Add(Projections.SqlGroupProjection(formatedDateSql, formatedDateGroupBy, new[] { "mydate" }, new[] { NHibernateUtil.Int32 })))
.List();
}
}

关于tsql - 如何使用 NHibernate ICriteria 进行分组、获取关联和 T-SQL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2386480/

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