在Fluent NHibernate中,是否可以将参数添加到List<int>
类型的过滤器中,以便过滤条件生成WHERE SomeColumn IN (@x, @y, @z)
?
我的用例是根据发票的ID和发票行号列表获取发票及其部分行。我想急切地在与发票相同的往返途中获取线路。我假设它是这样做的,但我找不到参数类型的正确类型声明:
域对象:
public class Invoice {
public int Id {get;set;}
public List<InvoiceLine> Lines {get;set;}
}
public class InvoiceLine {
public int Id {get;set}
public int LineNumber {get;set;}
}
映射:
public class InvoiceMap : ClassMap<Invoice> {
public InvoiceMap() {
Id(x => x.Id);
HasMany(x => x.Lines).ApplyFilter<OnlyLinesWithNumbersFilter>();
}
}
public class InvoiceLineMap : ClassMap<InvoiceLine> {
public InvoiceLineMap() {
Id(x => x.Id);
Map(x => x.LineNumber);
}
}
过滤器定义:
public class OnlyLinesWithNumbersFilter : FilterDefinition
{
public OnlyLinesWithNumbersFilter()
{
WithName("OnlyLinesWithNumbers");
WithCondition("LineNumber IN (:LineNumbers)");
AddParameter("LineNumbers",?? What to put here ??);
}
}
查询:
var filterName = "OnlyLinesWithNumbers";
session.EnableFilter(filterName).SetParameterList("LineNumbers", new[] {1,2,3});
var query = session.QueryOver<Invoice>()
.Where(i => i.Id == 42)
.Fetch(i => i.Lines).Eager
.TransformUsing(new DistinctRootEntityResultTransformer());
var result = query.SingleOrDefault();
session.DisableFilter(filterName);
答案 0 :(得分:5)
Take-2
InvoiceLine invoiceLineAlias = null;
var list = session.QueryOver<Invoice>()
.Where(x => x.Id == 1)
.JoinQueryOver(x => x.Lines, () => invoiceLineAlias, JoinType.LeftOuterJoin)
.WhereRestrictionOn(() => invoiceLineAlias.LineNumber)
.IsIn(new List<int> { 1, 2, 3 })
.List();
制作了sql:
SELECT
this_.Id as Id2_1_,
invoicelin1_.Invoice_id as Invoice3_3_,
invoicelin1_.Id as Id3_,
invoicelin1_.Id as Id3_0_,
invoicelin1_.LineNumber as LineNumber3_0_
FROM
"Invoice" this_
left outer join
"InvoiceLine" invoicelin1_
on this_.Id=invoicelin1_.Invoice_id
WHERE
this_.Id = @p0
and invoicelin1_.LineNumber in (
@p1, @p2, @p3
);
@p0 = 1 [Type: Int32 (0)],
@p1 = 1 [Type: Int32 (0)],
@p2 = 2 [Type: Int32 (0)],
@p3 = 3 [Type: Int32 (0)]
答案 1 :(得分:1)
你可以写
var list = session.QueryOver<Invoice>()
.WhereRestrictionOn(p => p.SomeColumn)
.IsIn(someList)
.List();
答案 2 :(得分:1)
要将NHibernate Filters与数组一起使用,请将NHibernateUtil.Int32放在AddParameter方法中,所以这样做:
-S -emit-llvm
启用过滤器后,在SetParameterList中设置一个数组
public class OnlyLinesWithNumbersFilter : FilterDefinition
{
public OnlyLinesWithNumbersFilter()
{
WithName("OnlyLinesWithNumbers");
WithCondition("LineNumber IN (:LineNumbers)");
AddParameter("LineNumbers", NHibernateUtil.Int32);
}
}
在我的测试中,我使用NHibernate 4.0.0.400
答案 3 :(得分:0)
我认为这是正确的语法,我只是把它写在了我的头顶。 :)
InvoiceLine invoiceLine = null;
var result = session.QueryOver<Invoice>()
.Where(x => x.Id == 42)
.JoinQueryOver(x => x.InvoiceLines, () => invoiceLine)
.WhereRestrictionOn(x => x.ItemNumber)
.IsIn(new[] {1, 2, 3})
.SingleOrDefault();