LLBLGen Pro中的子查询

时间:2014-09-24 11:21:15

标签: sql llblgenpro

我是LLBL gen pro的新手,我必须使用LLBL gen pro编写Sub查询,但我没有得到任何如何编写此示例的示例。 这是我必须编写的查询,我知道如何定义字段,关系和谓词表达式。

select UserId, TransactionId from 
UserTransaction
where TransactionId in
(
select  MaxId as TransactionId from
(SELECT MAX([TransactionId]) as MaxId, userid
FROM [UserTransaction]
group by userid) u1
inner join [UserTransaction] c2 on u1.MaxTId = c2.CCLeadTransactionId
where UserTypeId in (4,5)
)

这是我的表结构

UserTransaction
TransactionId userid UserTypeId

如何编写此查询

  select  MaxId as TransactionId from
    (SELECT MAX([TransactionId]) as MaxId, userid
    FROM [UserTransaction]
    group by userid) u1
    inner join [UserTransaction] c2 on u1.MaxTId = c2.CCLeadTransactionId
    where UserTypeId in (4,5)

1 个答案:

答案 0 :(得分:0)

基本思想是创建内部查询,父查询可以使用

下面的示例代码(尚未对其进行测试,可能包含拼写错误)

var qf = new QueryFactory(); // create your create factory

var subFilterQuery = qf.Create()
    .Select(()=> UserTransaction.TransactionId.SetAggregateFunction(AggregateFunction.Max).ToValue<int>()) // select max transaction value
    // do joins, etc
    .GroupBy(UserTransactionFields.UserId);

var parentQuery = qf.Create()
    .Select(()=> new {
        UserId = UserTransactionFields.UserId.ToValue<int>(),
        TransactionId = UserTransactionFields.TransactionId.ToValue<int>(),
    })
    .Where(UserTransactionFields.TransactionId.In(subFilterQuery.ToScalar())); // use create query as filter