如何在 NHibernate Criteria 中转换此 Lambda 表达式
我有这个类,
public class MyClass
{
public int Id { get; set;}
public string Name { get; set; }
public IList<MyClass> Classes { get; set; }
}
我正在使用 Fluent NHibernate,并且我有这个 AutoMappingOverride:
public void Override(AutoMapping<MyClass> mapping)
{
mapping.HasManyToMany(m => m.Classes)
.ParentKeyColumn("ClassId")
.ChildKeyColumn("SecondClassId")
.Table("ClassesRelation")
.Cascade.SaveUpdate();
}
我的存储库上有这个方法:
public IList<MyClass> ClassesThatContains(MyClass @class)
{
//allClasses.Where(tempClass => tempClass.Classes.Contains(@class))
string query = string.Format(@"SELECT Class.* FROM Class WHERE Class.Id IN (SELECT ClassesRelation.ClassId FROM ClassesRelation WHERE ClassesRelation.SecondClassId = {0})", @class.Id);
var criteria = NhSession.CreateSQLQuery(query).AddEntity(typeof(MyClass));
return criteria.List<MyClass>();
}
在 ClassesThatContains 方法中,我有相当于 SQL 语句的注释,但 NHibernate 不知道 Contains Linq 方法...所以我使用了SQL语句,但是如何将这个SQL语句转换为Criteria之类的?
观察:ClassRelation 不是类。
I have this class
public class MyClass
{
public int Id { get; set;}
public string Name { get; set; }
public IList<MyClass> Classes { get; set; }
}
I am using Fluent NHibernate, and I have this AutoMappingOverride:
public void Override(AutoMapping<MyClass> mapping)
{
mapping.HasManyToMany(m => m.Classes)
.ParentKeyColumn("ClassId")
.ChildKeyColumn("SecondClassId")
.Table("ClassesRelation")
.Cascade.SaveUpdate();
}
And I have this method on my Repository:
public IList<MyClass> ClassesThatContains(MyClass @class)
{
//allClasses.Where(tempClass => tempClass.Classes.Contains(@class))
string query = string.Format(@"SELECT Class.* FROM Class WHERE Class.Id IN (SELECT ClassesRelation.ClassId FROM ClassesRelation WHERE ClassesRelation.SecondClassId = {0})", @class.Id);
var criteria = NhSession.CreateSQLQuery(query).AddEntity(typeof(MyClass));
return criteria.List<MyClass>();
}
In the method ClassesThatContains I have the comment that is equivalent to SQL Statement, but NHibernate dont know Contains Linq method....So I used SQL Statement, but how can I convert this SQL Statement to Criteria like?
Obs: ClassRelation is not a Class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Nhibernate 在版本 3 中内置了一个 linq 提供程序,并在版本 2 中作为插件。
您可以像这样更改方法:
或者更接近您实际想要的查询的方法。
Nhibernate has a linq provider in version 3 built in and as an addon in version 2.
You could just change the method like so::
Or something more closer to your query you actually want.