【问题标题】:Fluent NHibernate: Order by in left join with conditionsFluent NHibernate:在左连接条件下排序
【发布时间】:2013-02-23 19:29:31
【问题描述】:

假设有以下映射类:

public class Item
{
  public virtual int Id { get; set; }
  public virtual IEnumerable<History> Histories { get; set; }
}

public class History
{
  public virtual int Id { get; set; }
  public virtual Item Item { get; set; }
  public virtual DateTime Date { get; set; }
  public virtual HistoryType HistoryType { get; set; }
}

public enum HistoryType
{
  A = 1,
  B = 2
}

现在我希望能够获取按最新历史日期排序的所有项目,其中 History 的 HistoryType = A。并非所有项目都有历史记录,所以我想需要左连接。

我需要的是一个 Fluent NHibernate 查询,但如果能看到一个正确的 SQL 查询也很好。

【问题讨论】:

    标签: c# sql nhibernate fluent-nhibernate left-join


    【解决方案1】:

    您的案例的标准 SQL 查询将如下所示。是的,您需要Left Join 才能获取所有项目。

    所有按其最新历史日期排序的项目,其中 History 的 HistoryType = A。即使是没有历史记录的项目

    SELECT i.id, h.id, h.datetime, h.historytype
    FROM ITEMS i
    LEFT JOIN HISTORY h
    ON i.id = h.itemid
    WHERE h.HistoryType = 'A'
    ORDER BY h.Datetime DESC
    

    如果您选择显示用户定义的值而不是 null。例如。当项目没有历史记录时,返回的历史记录表记录将为空。因此,像Colasce 这样的函数可以帮助您add syntatic sugar to your query :)

    请为 Fluent NHibernate 应用正确的语法(例如,是否对 String/varchar 列值使用反引号/反逗号)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多