In Fluent NHibernate, is it possible to add a parameter to a filter of type List<int>
so that the filter condition generates a WHERE SomeColumn IN (@x, @y, @z)
?
My use case is to fetch an invoice and a subset of its lines, given the ID of the invoice and a list of invoice line numbers. I want to eager fetch the lines in the same roundtrip as the invoice. I assume it is done something like this, but I cannot find the correct type declaration for the parameter type:
Domain objects:
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;}
}
Mappings:
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);
}
}
Filter definition:
public class OnlyLinesWithNumbersFilter : FilterDefinition
{
public OnlyLinesWithNumbersFilter()
{
WithName("OnlyLinesWithNumbers");
WithCondition("LineNumber IN (:LineNumbers)");
AddParameter("LineNumbers",?? What to put here ??);
}
}
Query:
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);