[英]Make a SQL Query to Nhibernate
如何獲得此SQL查詢到Nhibernate?
SELECT Customer.name
FROM Company INNER JOIN Customer ON Company.CompanyId = Customer.CompanyId
where CompanyId = 2
如果您熟悉LINQ,它非常非常簡單,
您必須直接訪問作為實體提交的參考。 然后您將獲得該實體的屬性,依此類推,直到第n個節點為止。
Nhibernate將把參考字段當作實體。
//Here is the sample this may work
//CustomerList is a nhibernate entity list.
//CompanyId is also an entity which is a reference to the CompanyId of Company table.
// you will get the list of customers based on condition.
var CustomerList = new List<Customer>();
var custList = from cust in CustomerList where cust.CompanyId.CompanyId == 2 select cust;
假設您有一個代表ID為2的公司。可以使用ICriterion:
return this.GetSession().CreateCriteria<Customer>()
.Add(Restrictions.Eq("Company", company))
.List<Customer>();
這將返回與公司關聯的客戶的列表(假設“客戶”類的屬性稱為“公司”)。
要獲取名稱,請執行以下操作:
customers.Select(c => c.name);
我建議使用這種方法,因為您知道所有客戶都將在一個數據庫命中中加載,而不是一次一次地懶惰地加載它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.