ServiceStack.ORMLite:自定义查询自定义Poco与Sql.In选择?

时间:2014-03-20 15:38:48

标签: sql sql-server database ormlite-servicestack

背景

我正在尝试使用ServiceStack.OrmLite来获取一些值(所以我可以缓存它们以对它们运行一些处理)。

我需要抓取三个值的组合,并且我有一个自定义SQL语句将产生它们(连接等)。

因为这将是一个很大的组合列表,我想传入一些值列表并使用Sql.In过滤到只有具有这些值的结果。

具体细节

我需要检查发票是否对公司和其他值(在此称为ClaimLawsuitID)是唯一的。

我的poco也是如此:

public class FirmIDClaimLawsuitIDInvoiceNumberCombination
{
    public string FirmID { get; set; }
    public string ClaimLawsuitID { get; set; }
    public string InvoiceNumber { get; set; }
}

我有我的SQL语句:

select tblDefenseInvoice.FirmID, tblDefInvClaimantDetail.ClaimLawsuitID, tblDefInvClaimantDetail.invoiceNumber 
    from tblDefenseInvoice
    inner join tblDefInvClaimantDetail
        on(tblDefenseInvoice.DefenseInvoiceID = tblDefInvClaimantDetail.DefenseInvoiceID)

我想运行以下内容:

 public List<FirmIDClaimLawsuitIDInvoiceNumberCombination> GetFirmIDClaimLawsuitIDInvoiceNumberCombinationsForExistingItems(IEnumerable<int> firmIds, IEnumerable<long> claimLawsuitIDs, IEnumerable<string> invoiceNumbers)
 {
     var sql = @"select tblDefenseInvoice.FirmID, tblDefInvClaimantDetail.ClaimLawsuitID, tblDefInvClaimantDetail.invoiceNumber 
            from tblDefenseInvoice
            inner join tblDefInvClaimantDetail
                on(tblDefenseInvoice.DefenseInvoiceID = tblDefInvClaimantDetail.DefenseInvoiceID)";

    var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<tblClaimLawsuit>();
    var firmFilter = PredicateBuilder.True<tblDefenseInvoice>();
    var claimLawsuitFilter = PredicateBuilder.True<tblDefInvClaimantDetail>();
    var invoiceNumberFilter = PredicateBuilder.True<tblDefInvClaimantDetail>();

    firmFilter = x => Sql.In(x.FirmID, firmIds);
    claimLawsuitFilter = x => Sql.In(x.ClaimLawsuitID, claimLawsuitIDs);
    invoiceNumberFilter = x => Sql.In(x.InvoiceNumber, invoiceNumbers);
    ev.Select(sql);
    ev.Where(firmFilter);
    ev.Where(claimLawsuitFilter);
    ev.Where(invoiceNumberFilter);

    return dal.DB.Select<FirmIDClaimLawsuitIDInvoiceNumberCombination>(ev.ToSelectStatement());

 }

问题

  • 这有可能达到这个目的吗?
  • 在ServiceStack的OrmLite中是否有其他方法可以实现这一点,它更清洁,我不知道?

1 个答案:

答案 0 :(得分:0)

由于我选择了POCO,我只需要根据POCO添加过滤器。

以下工作正常:

public List<FirmIDClaimLawsuitIDInvoiceNumberCombination>
    GetFirmIDClaimLawsuitIDInvoiceNumberCombinationsForExistingItems(
    IEnumerable<long> firmIds,
    IEnumerable<long> claimLawsuitIDs)
{
    var sql = @"select tblDefenseInvoice.FirmID, tblDefInvClaimantDetail.ClaimLawsuitID, tblDefInvClaimantDetail.invoiceNumber 
            from tblDefenseInvoice
            inner join tblDefInvClaimantDetail
                on(tblDefenseInvoice.DefenseInvoiceID = tblDefInvClaimantDetail.DefenseInvoiceID)";

    var ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<FirmIDClaimLawsuitIDInvoiceNumberCombination>();
    var firmFilter = PredicateBuilder.True<FirmIDClaimLawsuitIDInvoiceNumberCombination>();
    var claimLawsuitFilter = PredicateBuilder.True<FirmIDClaimLawsuitIDInvoiceNumberCombination>();

    firmFilter = x => Sql.In(x.FirmID, firmIds);
    claimLawsuitFilter = x => Sql.In(x.ClaimLawsuitID, claimLawsuitIDs);
    ev.Select(sql);
    ev.Where(firmFilter);
    ev.Where(claimLawsuitFilter);

    return dal.DB.Select<FirmIDClaimLawsuitIDInvoiceNumberCombination>(ev.ToSelectStatement());
}