public IList GetCustomersByFirstname(string firstname)
{
//写法1
//return _session
// .CreateQuery("select from Customer c where c.Firstname='" + firstname + "'")
// .List();
//写法2:位置型参数
//return _session
// .CreateQuery("select from Customer c where c.Firstname=?")
// .SetString(0, firstname)
// .List();
//写法3:命名型参数(推荐)
return _session
.CreateQuery("select from Customer c where c.Firstname=:fn")
.SetString("fn", firstname)
.List();
}
[Test]
public void GetCustomerByFirstnameTest()
{
IList customers = _queryHQL.GetCustomersByFirstname("YJingLee");
Assert.AreEqual(1, customers.Count);
foreach (var c in customers)
{
Assert.AreEqual("YJingLee", c.Firstname);
}
}
实例2:获取顾客ID大于CustomerId的顾客:
public IList GetCustomersWithCustomerIdGreaterThan(int customerId)
{
return _session
.CreateQuery("select from Customer c where c.CustomerId > :cid")
.SetInt32("cid", customerId)
.List();
}
public class CourseSorter:IComparer
{
#region IComparer Members
public int Compare(CourseDto x, CourseDto y)
{
return x.Title.CompareTo(y.Title);
}
#endregion
}
(T) expression // cast expression to be of type T
函数风格(Function-style)强制转型使用这样的语法:
T(expression) // cast expression to be of type T
这两种形式之间没有本质上的不同,它纯粹就是一个把括号放在哪的问题。我把这两种形式称为旧风格(old-style)的强制转型。
二、 C++的四种强制转型形式:
C++ 同时提供了四种新的强制转型形式(通常称为新风格的或 C++ 风格的强制转型):
const_cast(expression)
dynamic_cast(expression)
reinterpret_cast(expression)
static_cast(expression)
If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0.
If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.