none
Linq skips a field in translation RRS feed

  • Question

  • I have a query that combines results from two complex queries:

    var query1 =
        from i in GetComplexQuery1()
        select new Domain
        {
            ObjectWithManyFields = i.Object,
            Property1 = i.Property1, 
            Property2 = i.Property2, 
        };
    
    // translated to
    // SELECT <all Object fields>, [t4].[Property5], [t4].[value] FROM ... 
    
    var query2 =
        from i in GetComplexQuery2()
        select new Domain
        {
            ObjectWithManyFields = j.Object,
            Property1 = j.Object.SomeProperty, 
            Property2 = 0, 
        };
    
    // translated to 
    // SELECT <all Object fields>, @p6 AS [value3] FROM ... 
    
    var query = query1.Concat(query2);

    The translation does not contain SomeProperty, the result of that triggered the "All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists." exception.

    The only explanation I came up with is that SomeProperty already included in the list of fields of Object, so Linq doesn't bother to create an extra alias.  

    Can anyone explain why the translator is doing this? Thanks.

    Monday, November 25, 2013 9:38 PM

Answers

  • Hello,

    >>The only explanation I came up with is that SomeProperty already included in the list of fields of Object, so Linq doesn't bother to create an extra alias.

    Yes, you are right.

    The error is actually a SQL error and is normally caused by the fields retrieved for UNION is not the same for the 2 queries. For example, if the first query might have 3 fields, but the second query has 4 fields, this error will occur.

    In LINQ, this is most likely the result of a known issue. When a column value is referenced twice, it gets optimized out of the result set (even though you may need it to make unions line up). See it here.

    To fix this, we should make sure that all the starements using UNION, INTERSECT or EXCEPT operator have same number of expressions(columns).

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by xinonnet Tuesday, November 26, 2013 7:57 PM
    Tuesday, November 26, 2013 2:05 AM
    Moderator

All replies

  • Hello,

    >>The only explanation I came up with is that SomeProperty already included in the list of fields of Object, so Linq doesn't bother to create an extra alias.

    Yes, you are right.

    The error is actually a SQL error and is normally caused by the fields retrieved for UNION is not the same for the 2 queries. For example, if the first query might have 3 fields, but the second query has 4 fields, this error will occur.

    In LINQ, this is most likely the result of a known issue. When a column value is referenced twice, it gets optimized out of the result set (even though you may need it to make unions line up). See it here.

    To fix this, we should make sure that all the starements using UNION, INTERSECT or EXCEPT operator have same number of expressions(columns).

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by xinonnet Tuesday, November 26, 2013 7:57 PM
    Tuesday, November 26, 2013 2:05 AM
    Moderator
  • To fix this, we should make sure that all the starements using UNION, INTERSECT or EXCEPT operator have same number of expressions(columns).

    How does one do that then when using Linq2SQL?

    Monday, September 8, 2014 1:50 PM