Unable to Select after GroupBy based on 2 different tables

Hi, I’m trying to execute this query


return await ctx.SelectAsync<SchedulerOrderBaseInfo>(ctx
    .From<Order_ST>()
    .Join<Order_ST, ScheduledOrder>((o1, so1) => o1.Code == so1.OrderCode)
    .Join<ScheduledOrder, ScheduledOrderMachine>((so2, som2) => so2.Id == som2.IdOrder)
    .Join<ScheduledOrderMachine, ScheduledOrderMachineProcess>((som3, somp3) => som3.Id == somp3.IdOrderMachine)
    .Join<ScheduledOrderMachineProcess, ScheduledOrderMachineProcessRepetition>((somp4, sompr4) => somp4.Id == sompr4.IdScheduledOrderMachineProcess)
    .Where<Order_ST, ScheduledOrderMachineProcessRepetition>((o5, sompr5) =>
        o5.Status != OrderStatusArchived &&
        o5.Status != OrderStatusCancelled &&
        o5.IsLastRevision &&
        sompr5.StartDate <= intervalFinishDate &&
        sompr5.FinishDate >= intervalBeginDate)
    .GroupBy<ScheduledOrder, ScheduledOrderMachine>((so6, som6) => new { so6.OrderCode, som6.MachineCode })
    .Select<ScheduledOrder, ScheduledOrderMachine, ScheduledOrderMachineProcessRepetition>((so7, som7, sompr7) => new SchedulerOrderBaseInfo()
    {
        OrderCode = so7.OrderCode,
        MachineCode = som7.MachineCode,
        DoStart = Sql.Min(sompr7.StartDate),
        DoEnd = Sql.Max(sompr7.FinishDate)
    }));

but I receive this error

InvalidOperationException: variable ‘so7’ of type ‘Mares.Data.Entities.ScheduledOrder’ referenced from scope ‘’, but it is not defined

what I’m doing wrong? I’m sure I’m doing something wrong, but I cannot find where…

Thanks
Enrico

Can you put together a stand-alone example I can compile/run to repro the issue please?

e.g. like a runnable example on gistlyn

Hi mythz,
I solved it during the night. I change the query in this way

return await ctx.SelectAsync<SchedulerOrderBaseInfo>(ctx
                    .From<Order_ST>()
                    .Join<Order_ST, ScheduledOrder>((o, so) => o.Code == so.OrderCode)
                    .Join<ScheduledOrder, ScheduledOrderMachine>((so, som) => so.Id == som.IdOrder)
                    .Join<ScheduledOrderMachine, ScheduledOrderMachineProcess>((som, somp) => som.Id == somp.IdOrderMachine)
                    .Join<ScheduledOrderMachineProcess, ScheduledOrderMachineProcessRepetition>((somp, sompr) => somp.Id == sompr.IdScheduledOrderMachineProcess)
                    .Where<Order_ST, ScheduledOrderMachineProcessRepetition>((o, sompr) =>
                        o.Status != OrderStatusArchived &&
                        o.Status != OrderStatusCancelled &&
                        o.IsLastRevision &&
                        sompr.StartDate <= intervalFinishDate &&
                        sompr.FinishDate >= intervalBeginDate)
                    .GroupBy<ScheduledOrder, ScheduledOrderMachine>((so, som) => new
                    {
                        so.OrderCode,
                        som.MachineCode
                    })
                    .Select<ScheduledOrder, ScheduledOrderMachine, ScheduledOrderMachineProcessRepetition>((so, som, sompr) => new 
                    {
                        so.OrderCode,
                        som.MachineCode,
                        DoStart = Sql.Min(sompr.StartDate),
                        DoEnd = Sql.Max(sompr.FinishDate)
                    }));

the difference lay in the last select. In the first query I wrote

 .Select<ScheduledOrder, ScheduledOrderMachine, ScheduledOrderMachineProcessRepetition>((so7, som7, sompr7) => new SchedulerOrderBaseInfo()
    {
        OrderCode = so7.OrderCode,
        MachineCode = som7.MachineCode,
        DoStart = Sql.Min(sompr7.StartDate),
        DoEnd = Sql.Max(sompr7.FinishDate)
    }))

trying to create a concrete object of type SchedulerOrderBaseInfo, while in the working one I wrote

.Select<ScheduledOrder, ScheduledOrderMachine, ScheduledOrderMachineProcessRepetition>((so, som, sompr) => new 
                    {
                        so.OrderCode,
                        som.MachineCode,
                        DoStart = Sql.Min(sompr.StartDate),
                        DoEnd = Sql.Max(sompr.FinishDate)
                    }));

creating an anonimous type and letting ServiceStack cast the required object.

In this way the query works perfectlly and the SQL statement is exactly the same I wrote in SSMS.

Thanks again for your help
Enrico

ok cool glad you’ve resolved it, yeah the issue is that you’d need to select an anonymous type for your custom projection.