C#Lambda表达式和NHibernate

编程入门 行业动态 更新时间:2024-10-19 01:27:47
本文介绍了C#Lambda表达式和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)或将来的版本中,有没有类似的东西?

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

本文发布于:2023-11-08 07:32:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表达式   Lambda   NHibernate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!