c# – 带参数化IN子句的FluentNHibernate过滤器

前端之家收集整理的这篇文章主要介绍了c# – 带参数化IN子句的FluentNHibernate过滤器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在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);

解决方法

就拿-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,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)]
原文链接:https://www.f2er.com/csharp/99990.html

猜你在找的C#相关文章