Make a SQL Query to Nhibernate

前端 未结 2 1783
花落未央
花落未央 2021-01-29 01:25

How do I get this SQL query to Nhibernate?

SELECT Customer.name
FROM Company INNER JOIN Customer ON Company.CompanyId = Customer.CompanyId
where CompanyId = 2
         


        
2条回答
  •  一整个雨季
    2021-01-29 01:51

    Assuming you've got a company which represents the company with ID 2. You can use an ICriterion:

    return this.GetSession().CreateCriteria()
        .Add(Restrictions.Eq("Company", company))
        .List();
    

    This will return a list of the Customers associated with the company (assuming the property on the Customer class is called "Company").

    To get the names just do:

    customers.Select(c => c.name);
    

    I'd suggest this approach as you know all the customers will be loaded in one db hit rather than lazily loading them one at a time.

提交回复
热议问题