NHibernate 3 - 输入安全的方式来选择不同的值列表
我试图从表中选择一个不同的值列表,同时在另一列上排序。
到目前为止,唯一对我有用的方法是使用魔术字符串和对象数组。有更好的(类型安全)方法吗?
var projectionList = Projections.ProjectionList();
projectionList.Add(Projections.Property("FolderName"));
projectionList.Add(Projections.Property("FolderOrder"));
var list = Session.QueryOver<T>()
.Where(d => d.Company.Id == SharePointContextHelper.Current.CurrentCompanyId)
.OrderBy(t => t.FolderOrder).Asc
.Select(Projections.Distinct(projectionList))
.List<object[]>()
.ToList();
return list.Select(l => new Folder((string)l[0])).ToList();
顺便说一句,使用 linq 执行此操作是行不通的,您必须选择FolderOrder,否则您将收到 sql 错误(如果指定了 SELECT DISTINCT,则 ORDER BY 项必须出现在选择列表中。
)
然后这样做会产生一个已知错误:此 SelectClauseVisitor 不支持表达式类型“NhDistinctExpression”。
关于使用具有 unique
var q = Session.Query<T>()
.Where(d => d.Company.Id == SharePointContextHelper.Current.CurrentCompanyId)
.OrderBy(d => d.FolderOrder)
.Select(d => new {d.FolderName, d.FolderOrder})
.Distinct();
return q.ToList().Select(f => new Folder(f));
All 的匿名类型似乎需要很多麻烦和复杂性来完成一些 sql 基础知识。 ..
I am trying to select a distinct list of values from a table whilst ordering on another column.
The only thing working for me so far uses magic strings and an object array. Any better (type-safe) way?
var projectionList = Projections.ProjectionList();
projectionList.Add(Projections.Property("FolderName"));
projectionList.Add(Projections.Property("FolderOrder"));
var list = Session.QueryOver<T>()
.Where(d => d.Company.Id == SharePointContextHelper.Current.CurrentCompanyId)
.OrderBy(t => t.FolderOrder).Asc
.Select(Projections.Distinct(projectionList))
.List<object[]>()
.ToList();
return list.Select(l => new Folder((string)l[0])).ToList();
btw, doing it with linq won't work, you must select FolderOrder otherwise you'll get a sql error (ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
)
and then doing that gives a known error : Expression type 'NhDistinctExpression' is not supported by this SelectClauseVisitor.
regarding using anonymous types with distinct
var q = Session.Query<T>()
.Where(d => d.Company.Id == SharePointContextHelper.Current.CurrentCompanyId)
.OrderBy(d => d.FolderOrder)
.Select(d => new {d.FolderName, d.FolderOrder})
.Distinct();
return q.ToList().Select(f => new Folder(f));
All seems a lot of hoops and complexity to do some sql basics....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了解决类型安全问题,语法是:
To resolve the type-safety issue, the syntax is:
object []
的事情是不可避免的,除非您定义一个特殊的类/结构来仅保存FolderName
和FolderOrder
。请参阅这篇精彩的QueryOver 简介< /a> 用于类型安全,这是肯定受支持的。
祝你好运。
the
object []
thing is unavoidable, unless you define a special class / struct to hold justFolderName
andFolderOrder
.see this great introduction to QueryOver for type-saftey, which is most certainly supported.
best of luck.