這裏是QueryOver
版本。
我們正在使用別名,即:null參考Employee employee = null
的聲明,用於對所有屬性的完全類型訪問。 NHibernate表達式解析器將在稍後根據映射將它們轉換爲字符串(列名稱)。
另外,我們可以獲得對QueryOver的FROM部分的引用。 查詢代表Employee,加入的查詢代表部門(depQuery
),我們可以直接過濾。最後,我們可以使用List()來獲取(SELECT)所有映射的屬性,或者做一些投影:.Select()
或.SelectList()
。通過投影,我們應該與一些DTO合作。
// aliasing, see the Projections of the SELECT clause
Employee employee = null;
Department department = null;
// the Employee query, with alias
var query = session.QueryOver<Employee>(() => employee);
// this way we can have reference to department query, if needed
var depQuery = query.JoinQueryOver<Department>(() => employee.Department,() => department);
// WHERE
// filtering the Employee
query.Where(e => e.Name == "Undefined");
// the department filtering
depQuery.Where(d => d.DeptName == "Management");
// paging, if needed
query.Skip(100);
query.Take(10);
1)選擇所有屬性
var list = query.List<Employee>();
var employeeName = list.ElementAt(0).Name;
var departmentName = list.ElementAt(0).Department.DeptName;
2)投影
// The DTO class to be projected into
public class MyDTO
{
public virtual string EmployeeName { get; set; }
public virtual string DepartmentName { get; set; }
}
// Select with projection of just two columns
MyDTO dto = null;
// SELECT
// projection, explicit property/column to be selected only
query.SelectList(l => l
// the full power of aliasing
.Select(() => employee.Name).WithAlias(() => dto.EmployeeName)
.Select(() => department.DeptName).WithAlias(() => dto.DepartmentName)
);
var list = query
.TransformUsing(Transformers.AliasToBean<MyDTO>())
.List<MyDTO>();
這應該是相當簡單的,但你會學到很多東西更多,如果你嘗試去解決它自己第一:你嘗試過什麼,發生了什麼?請使用您已經嘗試過的LINQ查詢更新您的問題,以及您收到的任何錯誤等。 –
好的我正在更新。 –
@Undefiend無需更新。你的問題擱置。下次再試試更好的問題 –