我是NHibernate伟大世界中的新手.我正在使用2.0.1.GA版本.这是我的问题.我有一个表Cars和列Manufacturer(nvarchar(50))和一个主键ID(int).我的.NET类是:
I'm a newbie in the great world of NHibernate. I'm using version 2.0.1.GA. Here's my question. I have a table Cars with column Manufacturer(nvarchar(50)) and a primary key ID(int). My .NET class is:
public class Car { public virtual int ID { get; set; } public virtual string Manufacturer { get; set; } }现在,如果要检索梅赛德斯制造的所有汽车,则必须输入以下内容:
Now if I want to retrieve all cars made by Mercedes I have to type this:
using (var session = OpenSession()) { var cars = session .CreateCriteria(typeof(Car)) .Add(Restrictions.Like("Manufacturer", "Mercedes")) .List(); // ... }我不喜欢将属性名称指定为字符串的事实:( 可能有一些更适合重构的东西(这只是一个建议)?
I don't like the fact that I need to specify the property name as a string :( Is it possible to have something more refactor friendly probably (it's only a suggestion)?
var ms = session .CreateCriteria<Car>() .Add(c => c.Manufacturer, Restrictions.Like("Mercedes") .List();在当前版本(2.0.1.GA)或将来的版本中,有没有类似的东西?
Anything like thins in the current version (2.0.1.GA) or in a future version?
推荐答案就像Google Ninja所说的那样,您可以使用NHibernate.Linq做到这一点. 该查询将是:
Like Google Ninja said, you can do it with NHibernate.Linq. The query would then be:
session.Linq<Car>.Where(c => c.Manufacturer == "Mercedes").ToList()
如果有人最终使用NH3.0,则语法略有不同(感谢Michael Mrozek和Mike的建议):
If someone ends up here and is using NH3.0 the syntax is just a tad different (thanks to Michael Mrozek and Mike for the suggestion):
session.Query<Car>.Where(c => c.Manufacturer == "Mercedes").ToList()
我使用了与 fluent-nhibernate 捆绑在一起的二进制文件,适用于2.0GA(我认为不确定具体版本).
I've used a binary that came bundled with fluent-nhibernate that works with 2.0GA (I think, not sure about the particular revision).
更多推荐
C#Lambda表达式和NHibernate
发布评论