C# Lambda 表达式和 NHibernate [英] C# Lambda expressions and NHibernate
问题描述
我是 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) 或未来版本中是否有类似 Thins 的东西?
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
如果有人最终在这里使用 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
我使用了一个与 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!