NHibernate 3 - 输入安全的方式来选择不同的值列表

发布于 2024-12-03 08:43:47 字数 1191 浏览 1 评论 0原文

我试图从表中选择一个不同的值列表,同时在另一列上排序。

到目前为止,唯一对我有用的方法是使用魔术字符串和对象数组。有更好的(类型安全)方法吗?

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-12-10 08:43:47

为了解决类型安全问题,语法是:

  var projectionList = Projections.ProjectionList();
  projectionList.Add(Projections.Property<T>(d => d.FolderName));
  projectionList.Add(Projections.Property<T>(d => d.FolderOrder));

To resolve the type-safety issue, the syntax is:

  var projectionList = Projections.ProjectionList();
  projectionList.Add(Projections.Property<T>(d => d.FolderName));
  projectionList.Add(Projections.Property<T>(d => d.FolderOrder));
盗琴音 2024-12-10 08:43:47

object [] 的事情是不可避免的,除非您定义一个特殊的类/结构来仅保存 FolderNameFolderOrder
请参阅这篇精彩的QueryOver 简介< /a> 用于类型安全,这是肯定受支持的。
祝你好运。

the object [] thing is unavoidable, unless you define a special class / struct to hold just FolderName and FolderOrder.
see this great introduction to QueryOver for type-saftey, which is most certainly supported.
best of luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文