none
Union of two linq queries RRS feed

  • Question

  • Hi ,

     

    I have two linq queries. for first query i am getting fields from database and for second query i am adding new column with value( allow =2). Here two queries are executing success fully but when i make union for the two queries it is giving compile error as

     

    "The type arguments for method 'System.Linq.Queryable.Union<TSource>(System.Linq.IQueryable<TSource>, System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly."

     

    My code as follows.

     

     var Query1 = from obj1 in Permission1
                                                     select new
                                                    {
                                                        obj1 .Group_ID,
                                                        obj1 .PERMISSION_ID,
                                                        obj1 .PERMISSION_NAME,
                                                        obj1 .ALLOW
                                                    };

    var Query2 = from obj2 in Permission2
                                                     select new
                                                    {
                                                        obj2 .Group_ID,
                                                        obj2 .PERMISSION_ID,
                                                        obj2 .PERMISSION_NAME,
                                                        ALLOW = 2
                                                    };

    var Query = Query1 .Union(Query2 );

     

    I am getting error at last line

     

     

    Thanks in advance.

     

    Regards,

    Shiva

     

    Wednesday, May 21, 2008 3:50 AM

Answers

  • Hello,

    For the compiler to successfully infer the type of the result of the union, the two anonymous types returned by Query1 and Query2 need to be identical (in fact, the compiler generates a single type). There is nothing obvious I can notice in your query besides the fact that the type of ojb1.ALLOW may not be exactly the same as the type of "2". If permission1 and permission2 are of different types, then the difference could be in other property.

    Hope this helps,
    Diego

    Wednesday, May 21, 2008 8:49 AM

All replies

  • Hello,

    For the compiler to successfully infer the type of the result of the union, the two anonymous types returned by Query1 and Query2 need to be identical (in fact, the compiler generates a single type). There is nothing obvious I can notice in your query besides the fact that the type of ojb1.ALLOW may not be exactly the same as the type of "2". If permission1 and permission2 are of different types, then the difference could be in other property.

    Hope this helps,
    Diego

    Wednesday, May 21, 2008 8:49 AM
  • Hi Diego,

     

    Great...It is solved..It is working now.obj1.ALLOW is decimal type. instead of passing directly value is '2' i created a decimal value and i assigned.

     

    Thanks.

    Shiva

     

    Code as follows

     

    decimal value =2;

    var Query1 = from obj1 in Permission1
                                                     select new
                                                    {
                                                        obj1 .Group_ID,
                                                        obj1 .PERMISSION_ID,
                                                        obj1 .PERMISSION_NAME,
                                                        obj1 .ALLOW
                                                    };

    var Query2 = from obj2 in Permission2
                                                     select new
                                                    {
                                                        obj2 .Group_ID,
                                                        obj2 .PERMISSION_ID,
                                                        obj2 .PERMISSION_NAME,
                                                        ALLOW = value                                                };

    var Query = Query1 .Union(Query2 );

     

     

    Wednesday, May 21, 2008 3:39 PM
  • Cheers mate, I had the same problem and I could solve it as you did.
    Tuesday, September 29, 2015 6:04 PM