14578446
14578446

Reputation: 1074

return Anonymous type from compiled query delegate

I want to return an anonymous type from a compiled query, which selects multiple columns from two tables.

I tried using:

public static Func < DBEntities, string> 

but not able to compile it. I tried creating a new datatype BOMWorkOrder but could not make it work. May be ia m missing some syntax.

public static Func<DBEntities, string, IQueryable<BOMWorkOrder>> compiledWorkorderQuery =
        CompiledQuery.Compile((DBEntities ctx, string bomNumber) =>
            from items in ctx.BM10200 
            from orders in ctx.BM10300
            where orders.Parent_Component_ID == -1 &&
                    orders.ITEMNMBR == bomNumber &&
                    orders.TRX_ID == items.TRX_ID
            select new 
            {  bomWorkOrder =
                items.TRXDATE,
                orders.TRX_ID,
                orders.ITEMNMBR,
                orders.Assemble_Quantity
            });

where work order will be:

public class BOMWorkOrder
{
    public DateTime TransactionDate { get; set; }
    public string TransactionId { get; set; }
    public string ItemNumber { get; set; }
    public int AssemblyQuantity { get; set; }
}

Upvotes: 2

Views: 1209

Answers (3)

Reza ArabQaeni
Reza ArabQaeni

Reputation: 4907

    select new bomWorkOrder
    {  
        TransactionDate =items.TRXDATE,
        TransactionId =orders.TRX_ID,
        ItemNumber =orders.ITEMNMBR,
        AssemblyQuantity =orders.Assemble_Quantity
    });

Upvotes: 0

phoog
phoog

Reputation: 43046

Since you've created the type BOMWorkOrder, use that type rather than an anonymous type:

... select new BOMWorkOrder 
    { 
        TransactionDate = items.TRXDATE,
        TransactionId = orders.TRX_ID,
        ItemNumber = orders.ITEMNMBR,
        AssemblyQuantity = orders.Assemble_Quantity
    };

Upvotes: 3

Nick Butler
Nick Butler

Reputation: 24383

If you return a list of anonymous objects, you will not be able to access the properties ( unless you use dynamic )

You're just missing the type name in your select:

...
select new BOMWorkOrder
{
    TransactionData = items.TRXDATE,
    TransactionId = orders.TRX_ID,
    ItemNumber = orders.ITEMNBBR,
    AssemblyQuantity = orders.Assemble_Queantity,
}

Upvotes: 0

Related Questions