What is the difference between 1 column linq result and more than 1 column linq result

Unanswered What is the difference between 1 column linq result and more than 1 column linq result

  • Thursday, February 28, 2013 10:32 AM
     
      Has Code

    This behavior make me crazy since I could not find a logical explanation of why ????

    All I want to do is 2 things
    1- Columns Count in Linq result
    2- Columns Name in Linq Result

    The STRANGE problem is, if my linq result is more than 1 column, it worked, if it 1 column it did not

    Dim query_3_col = From t In db.Customers Select t.CustomerID,t.City,t.Address
            Dim col_count_3_col As Integer
            col_count_3_col = query_3_col.ToArray(0).GetType.GetProperties().Count ' col_count_3_col = 3 <-- correct
                              
            Dim query_3_col_first = query_3_col.First()
            Dim prop_3_col = (from propertyy in query_3_col_first.GetType().GetProperties()
                            select propertyy.Name).ToList()
            For Each item In prop_3_col
                Debug.Print(item.ToString)
                ' I am getting CustomerID,City,Address <-- correct
            Next
    
            col_count_3_col = prop_3_col.Count ' col_count_3_col = 3 <-- correct

    Let do the above again with 1 column result

            Dim query_1_col = From t In db.Customers Select t.CustomerID
                              
            Dim col_count_1_col = query_1_col.ToArray(0).GetType.GetProperties().Count ' col_count_1_col = 2 <-- WRONG
    
            Dim query_1_col_first = query_1_col.First()
    
            Dim prop_1_col = (from propertyy in query_1_col_first.GetType().GetProperties()
                            select propertyy.Name).ToList()
            
            For Each item In prop_1_col
                Debug.Print(item.ToString)
                ' I am getting Chars,Length ???? What is that?
            Next
            col_count_1_col = prop_1_col.Count ' col_count_1_col = 2 <-- WRONG

    - I want to know how to tell that the linq result is 1 column

    - I want to get the column name in case of 1 column in the Linq result.

    Thanks


    .Net Blog VFP Blog


    • Edited by Samir Ibrahim Thursday, February 28, 2013 10:36 AM
    •  

All Replies

  • Friday, March 01, 2013 9:21 AM
    Moderator
     
     

    Hi Samir,

    Welcome to the MSDN forum.

    I guess the type of CustomerID is String. Is that right?

    For the first example, the compiler created an anonymous type which have three read-only properties to store each values (t.CustomerID,t.City,t.Address), so you can get three properties. For second, you will get the properties of type of CustomerID. For String type, it only have two properties “Char” and “Length”.

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Friday, March 01, 2013 10:03 AM
     
     

    Hi  Alexander

    +1 for your explanation, but I still find this behavior .... I will use the word "annoying"

    Yes, CustomerID is nchar(5) in Northwind SqlServer db and by linq mapping it is NChar(5) NOT NULL

    a Linq result should have standards or definition where here I can see it has two, first for 1 column and second for +1 column?

    ok, lets solve my problem..

    Dim LinqQueryResult = Something

    How I can tell if this is 1 column or +1 column and how to extract the column name in case it was 1 column?

    Thank you.


    .Net Blog VFP Blog