【问题标题】:How do I join tables with a condition using LLBLGen?如何使用 LLBLGen 连接带有条件的表?
【发布时间】:2009-03-31 13:45:09
【问题描述】:

我有以下 Sql 查询,它返回我想要的结果类型:

SELECT b.ID, a.Name, b.Col2, b.COl3
FROM Table1 a
LEFT OUTER JOIN Table2 b on b.Col4 = a.ID AND b.Col5 = 'test'

本质上,我希望行数等于表 1 (a),同时列出表 2 (b) 中的数据,如果条件“测试”在表 2 中不存在,则为 NULL。

我对 LLBLGen 比较陌生,并且尝试了一些方法,但它不起作用。如果条件存在,我可以让它工作;但是,当需求发生变化并导致我将查询重写为上述查询时,我不知所措。

以下是适用于现有产品但不适用于上述查询的旧 LLBLGen C# 代码:

LookupTable2Collection table2col = new LookupTable2Collection();

RelationCollection relationships = new RelationCollection();
relationships.Add(LookupTable2Entity.Relations.LookupTable1EntityUsingTable1ID, JoinHint.Left);

IPredicateExpression filter = new PredicateExpression();
filter.Add(new FieldCompareValuePredicate(LookupTable2Fields.Col5, ComparisonOperator.Equal, "test"));

table2col.GetMulti(filter, relationships);

表 1 中有 3 条记录。即使表 2 中的所有项目都为 NULL,我也需要返回 3 条记录,因为条件不存在。有什么想法吗?

【问题讨论】:

    标签: c# sql sql-server llblgenpro


    【解决方案1】:

    您必须像这样将过滤器添加到关系联接中:

    relationships.Add(LookupTable2Entity.Relations.LookupTable1EntityUsingTable1ID, JoinHint.Left).CustomFilter = new FieldCompareValuePredicate(LookupTable2Fields.Col5, ComparisonOperator.Equal, "test");
    

    【讨论】:

    • 嗯,如何在预取中做到这一点...?
    • 您还可以定义 PredicateExpression 并将 .CustomFilter 设置为此。 IPredicateExpression myFilter = new PredicateExpression(); myFilter.Add(CompanyFields.Name == "StackExchange"); productRelations.Add(ProductEntity.Relations.CompanyEntityUsingCompanyId, JoinHint.Left).CustomFilter = myFilter;
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2023-03-16
    • 1970-01-01
    相关资源
    最近更新 更多