gpt4 book ai didi

Nhibernate QueryOver。 OrderBy 使用字符串属性名称。

转载 作者:行者123 更新时间:2023-12-03 09:43:00 26 4
gpt4 key购买 nike

我正在重构旧式查询 CreateCriteria()QueryOver() .我的 Wcf 服务得到 string PropertyName对查询结果进行排序。对于 IQueryable我使用动态 LINQ 来做这样的排序,对于 CreateCriteria() - AddOrder() .

IList<object[]> result =
GetSession()
.QueryOver(() => activity)
.JoinAlias(() => activity.ActivityLicense, () => license)
.Select(Projections.ProjectionList()
.Add(Projections.Count<Activity>(e => e.Id), "ActivityCount")
.Add(Projections.Group(() => license.SerialNumber), "SerialNumber")
.Add(Projections.Count<Activity>(e => e.MacAdress), "MacAddressCount")
.Add(Projections.Count<Activity>(e => e.IpAdress), "IpAddressCount")
)
.OrderByAlias("ActivityCount") // Compilation Error - I need such extension method
.List<object[]>();

任何建议如何在字符串属性名称的情况下进行排序?

PS:我无法使用 LINQ to Nhibernate: LINQ to NHibernate - .GroupBy().Skip().Take() cause an exception

谢谢!

最佳答案

您可以随时获得 UnderlyingCriteria ...

var q = GetSession()
.QueryOver(() => activity)
.JoinAlias(() => activity.ActivityLicense, () => license)
.Select(Projections.ProjectionList()
.Add(Projections.Count<Activity>(e => e.Id), "ActivityCount")
.Add(Projections.Group(() => license.SerialNumber), "SerialNumber")
.Add(Projections.Count<Activity>(e => e.MacAdress), "MacAddressCount")
.Add(Projections.Count<Activity>(e => e.IpAdress), "IpAddressCount")
);


q.UnderlyingCriteria.AddOrder(new Order("ActivityCount", true));

var results = q.List();

或作为 IQueryOver 的扩展方法
public static IQueryOver<T,T> OrderByAlias(this IQueryOver<T,T> q, string aliasName, bool ascending)
{
q.UnderlyingCriteria.AddOrder(new Order(aliasName, ascending));
return q;
}

关于Nhibernate QueryOver。 OrderBy 使用字符串属性名称。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11935351/

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