How to do a SELECT DISTINCT equivalent for a page filter in NHibernate?

笑着哭i 提交于 2020-01-15 06:12:08

问题


Lets say you are working in SQL 2005 with a copy of Northwind database installed. Your working on an ASP.NET application with an Employees "browse" page. At the top of the page you have a "Title" filter where you would like to display these 5 choices in a dropdown:

[ALL]
Vice President, Sales
Sales Representative
Sales Manager
Inside Sales Coordinator

In T-SQL you would use something like the statement below to get your list.

SELECT DISTINCT Title FROM Employees ORDER BY Title

What is the best way of doing this in NHibernate? Assume that the initial database design is somewhat out of your control (just like Northwind)... Meaning that you won't be creating a Titles or Positions table for normalization.

Thanks.


回答1:


See nhibernate.info/doc/howto/various/get-unique-results-from-joined-queries.html




回答2:


ANSWER: I have 29 records in the table with 4 records repeated twice. I want only Distinct results. So, the total number of records should be 25.

below is test method

[TestMethod]
public void UserApplicationsTest()
{
    int usercount = 25;
    string query = "select from User u left outer join fetch u.ApplicationRequests ar";

    ISession session = NHibernateSessionManager.GetSession();


    IList<User> users = session.CreateQuery
       (
       query
       )
       .List<User>()
       .Distinct<User>().ToList();


    Assert.AreEqual(usercount, users.Count);

}



回答3:


criterion =  ... // SELECT Title FROM Employees ORDER BY Title
criterion.SetResultTransformer(new NHibernate.Transform.DistinctRootEntityResultTransformer())


来源:https://stackoverflow.com/questions/558974/how-to-do-a-select-distinct-equivalent-for-a-page-filter-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!