tags:

views:

59

answers:

1

I have the following SQL query, note I want the literal value '0' in the second field in the second SELECT statement from the ItemSale table. How do I express this in LINQ? I get the error message 'Invalid anonymous type member declarator'.

SELECT BranchNumber,QuantitySold
FROM Department 
UNION
SELECT BranchNumber,0
FROM ItemSale

How to express the '0' in LINQ?

var unionQuery = (from dept in Department
                            select new
                            {
                                dept.BranchNumber,
                                dept.QuantitySold,
                            })
                            .Concat(from item in ItemSale
                               select new
                               {
                                   item.BranchNumber,
                                   0
                               });
A: 

In LINQ, the field has to have a name that matches the corresponding field in the other anonymous type.

select new
{
   item.BranchNumber,
   QuantitySold=0
});
Marcelo Cantos
Unfortunately, this just causes the following error: The type arguments for method 'System.Linq.Queryable.Concat<TSource>(System.Linq.IQueryable<TSource>, System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
lowlyintern
Perhaps the types are mismatched. Try casting the `0` to whatever type `dept.QuantitySold` is.
Marcelo Cantos
unfortunately this does not work either.
lowlyintern
This works:QuantitySold = Convert.ToDouble(0),
lowlyintern
If it's a double, then just use `0.0`.
Marcelo Cantos