我正在查询 ProductRisk,它包含一个 Status 属性,其中 Status 是一个枚举。以下是 ProductRisk 的映射:
public class ProductRiskMap : ClassMap<ProductRisk>
{
public ProductRiskMap()
{
Table("AccountManagement.dbo.ProductRisk");
Id(x => x.Id, "ProductRiskID");
References(x => x.AssociatedProduct, "ProductID");
References(x => x.AssociatedClient, "EntityId");
Map(x => x.Reason, "ProductRiskReasonID").CustomType<int>();
Map(x => x.Status, "RiskStatusID").CustomType<int>();
}
Run Code Online (Sandbox Code Playgroud)
Status 是一个具有四个可能值的枚举。它在数据库中表示为对查找表的外键引用。在我的存储库中,我想提取状态为Medium或的 ProductRisk 对象High。Ling To NHibernate 中的以下查询有效:
public IList<ProductRisk> GetByClient(int[] clientIds)
{
return NHibernateSession.Current.Query<ProductRisk>()
.Where(x => clientIds.Contains(x.AssociatedClient.Id))
.Where(x => x.Status == RiskStatus.Medium || x.Status == RiskStatus.High)
.ToList();
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我在 Criteria API 中使用(我认为是)等效的查询:
return NHibernateSession.Current.QueryOver<ProductRisk>()
.WhereRestrictionOn(x => x.AssociatedClient.Id).IsIn(clientIds) …Run Code Online (Sandbox Code Playgroud) 当我执行以下查询时,我得到一个异常,告诉我'feedItemQuery'包含多个项目(因此SingleOrDefault不起作用).
当使用Criteria api而没有DistinctRootEntity转换器时,这是预期的行为,但是当使用linq时,我希望得到一个单一的根实体(FeedItem,其属性Ads(of ICollection)包含所有广告).
有没有办法告诉NHibernate.Linq使用DistinctRootEntity转换器?
我的查询:
var feedItemQuery = from ad in session.Linq<FeedItem>().Expand("Ads")
where ad.Id == Id
select ad;
var feedItem = feedItemQuery.SingleOrDefault(); // This fails !?
Run Code Online (Sandbox Code Playgroud)
映射:
<class name="FeedItem" table="FeedItems" proxy="IFeedItem">
<id name="Id" type="Guid">
<generator class="guid.comb"></generator>
</id>
...
<set name="Ads" table="Ads">
<key column="FeedItemId" />
<one-to-many class="Ad" />
</set>
</class>
Run Code Online (Sandbox Code Playgroud)
提前致谢
在这样的代码中:
if (insuranceNumberSearch == null
? true
: ei.InsuranceNumber.Contains(insuranceNumberSearch.Trim()))
doSomething();
Run Code Online (Sandbox Code Playgroud)
where insuranceNumberSearch为null,在以下代码中,其余表达式不为null:
var q = from ei in session.Linq<EmployeeInsurance>()
where insuranceNumberSearch == null
? true
: ei.InsuranceNumber.Contains(insuranceNumberSearch.Trim())
select ei;
Run Code Online (Sandbox Code Playgroud)
无论insuranceNumberSearch是null还是非null,都会计算表达式的所有部分.
我正在使用LINQ到NHibernate
更新:
不幸的是,我把第一个片段错了.正确的是:
if (insuranceNumberSearch == null || (insuranceNumberSearch != null && ei.InsuranceNumber.Contains(insuranceNumberSearch.Trim()))
doSomething();
Run Code Online (Sandbox Code Playgroud)
要么
bool b1 = insuranceNumberSearch == null ? true : ei.InsuranceNumber.Contains(insuranceNumberSearch.Trim());
if (b1)
doSomething();
Run Code Online (Sandbox Code Playgroud)
在两种以上时insuranceNumberSearch是null,剩余表达式不评估任何更多.如果这样的行为不存在,insuranceNumberSearch.Trim()将导致引用对象为空异常.遗憾的是,LINQ(或者LINQ-to-NHibernate)不服从这样一个好的行为并且即使在出现错误时insuranceNumberSearch也会评估所有表达式null.
更新2:我发现了一个类似的问题:|| (或)Linq中使用C#的操作员
我有2个像这样的对象
public class Child
{
public virtual int ChildId { get; set; }
}
public class Parent
{
public virtual int ParentId { get; set; }
public virtual IList<Child> Children { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我试图写一个linq到nhibernate查询来选择一个父类,其中包含一个具有特定id的子.return x => x.Children.Contains不起作用.我也尝试过这个.
return x => (from y in x.Children where y.ChildId.Equals(childId) select y).Count() > 0
Run Code Online (Sandbox Code Playgroud)
我的流畅映射看起来像这样
HasManyToMany<Child>(x => x.Children)
.Table("ParentsChildren")
.ParentKeyColumn("ParentId")
.ChildKeyColumn("ChildId");
Run Code Online (Sandbox Code Playgroud)
如何通过id找到包含子项的父项?
这个问题似乎有点出现,我还没有看到一个好的答案.我有两个没有外键的类,除了一个公共字段之外没有真正的关系,在本例中是"Title".
这基于我最近从遗留应用程序中提取的示例,我不允许修改架构,因此只是添加外键不是一种选择.我正在寻找的是一个查询,它将为具有给定标题的课程提供所有先决条件:
select p.* from course c join prereq p on c.title = p.title
Run Code Online (Sandbox Code Playgroud)
我不是在寻找像Join(),HasMany()等这样的映射,因为它们显然都需要一个已定义的关系.我希望基于没有映射的任意列连接两个表.
类似的问题在这里问了一段时间后似乎表明,它可能使用CreateAlias(),但我还没有找到任何很好的例子.
<class name="Course" table="course">
<id name="id" column="id" type="long">
<generator class="identity" />
</id>
<property name="Title" column="Title" type="String" />
</class>
<class name="Prereq" table="prereq">
<id name="id" column="id" type="long">
<generator class="identity" />
</id>
<property name="Title" column="Title" type="String" />
<property name="PrereqTitle" column="PrereqTitle" type="String" />
</class>
Run Code Online (Sandbox Code Playgroud)
这就是我想出来的,但它似乎没有用.有什么建议?
var results = session.CreateCriteria(typeof(Prereq))
.CreateAlias("Course", "C")
.CreateAlias("Prereq", "P")
.Add( Expression.EqProperty("C.Title", "P.Title"))
.Add( Expression.Eq("C.Title", "Course With Prereq"))
.List();
Run Code Online (Sandbox Code Playgroud)
这很容易与LinqToSql一起使用,可以用Liniber提供者为NHibernate完成吗?我看到的例子似乎表明提供者基本上对NH使用的任何ICriteria/ICriterion魔法进行了查询 - 它似乎不可能但如果我弄错了请纠正我.
.net nhibernate icriteria nhibernate-mapping linq-to-nhibernate
我有一个使用linq到NHibernate的查询,用于EnterAndExitArchive实体.该实体具有按Archive实体关联.
public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
var q = SessionInstance.Query<EnterAndExitArchive>()
.Where(x => x.Archive.Id == archiveId)
.LastOrDefault<EnterAndExitArchive>();
return q;
}
Run Code Online (Sandbox Code Playgroud)
要么
public EnterAndExitArchive GetLastEnterAndExitArchive(long archiveId)
{
var q = SessionInstance.Query<EnterAndExitArchive>()
.LastOrDefault<EnterAndExitArchive>(x => x.Archive.Id == archiveId);
return q;
}
Run Code Online (Sandbox Code Playgroud)
但这有一个运行时错误.异常消息是The LastResultOperator result operator is not current supported.
为什么?
我做了网络搜索,但我找不到可靠的答案.LINQ to NHibernate是否容易受到SQL注入和其他原始SQL攻击?如果是,那么说明如何避免此类数据库攻击的代码是什么?
我有一个Address类.
public class Address : RootEntityBase
{
virtual public string Province { set; get; }
virtual public string City { set; get; }
virtual public string PostalCode { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
通过此默认值:
var myList = new List<Address>
{
new Address {Province = "P1", City = "C1", PostalCode = "A"},
new Address {Province = "P1", City = "C1", PostalCode = "B"},
new Address {Province = "P1", City = "C1", PostalCode = "C"},
new Address {Province = "P1", City = "C2", …Run Code Online (Sandbox Code Playgroud) 我有两个单独的查询,都返回相同的IQueryable,我想在投影之前将它们合并。看起来在Linq to NHibernate中都没有实现Union或Concat?有谁知道我将如何实现这一目标?
我有3个类:Person,Employee1,Employee2
public class Employee1 : Person
{
}
public class Employee2 : Person
{
}
Run Code Online (Sandbox Code Playgroud)
我需要查询Person_Table,有时需要加入Employee1_Table或Employee_Table.
var q = SessionInstance.Query<Person>();
if (dto.Code != null) // A1 Condition
q = q.Where(x => x.Code == dto.Code);
//if(A2 Condition)
//if(A3 Condition)
//...
switch (dto.Type)
{
case PersonType.Employee1:
var q1 = SessionInstance.Query<Employee1>();
q.Join(q1, x => x.Id, xx => xx.Id, (x, xx) => x);
if (!String.IsNullOrEmpty(dto.Unit)) // B1 Condition
q1 = q1.Where(xx => xx.Unit == dto.Unit);
//if(B2 Condition)
//if(B3 Condition)
//...
return q1.ToList<Person>();
case PersonType.Employee2: …Run Code Online (Sandbox Code Playgroud)