none
DataContext.ExecuteQuery<TDTO> should only assign non null values RRS feed

  • Question

  • Hi,

    I am developing a multilingual admin tool. So every Entity has an EntityDetailsTable for language specific data like:

    Person and Person_Details

    A custom dto (PersonDTO) combines the information of these 2 tables.

    Now i am facing a problem when the user switches to a language where no Detail Tables exists. For sure i am fatching the Data with a left join but ExecuteQuery has troubles binding null values to int.

    Concreate Example:


    PersonID Firstname PersonID Description language-code
    1 Max null null null


    Person: PersonID, Firstname
    Person_Details: PersonID, Description, language-code

    PersonDTO: PersonID, Firstname, Description, language-code

    When I execute
    IList<PersonDTO> result = DataContext.ExecuteQuery<PersonDTO>(query.ToString()).ToList<PersonDTO>();

    Now i get the following error:

    The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.


    I think the best was is to tell DataContext not to assign any null values. Or should i use different column names, or i don't know.

    Workaround: Well i guess my approach of SELECT * (asterix) is wrong. If i don'T get the id twice then it is not going to be overwritten. So i have to build generic methods for retreiving the select fieldnames of Details without the pk of entity. Cumbersome but doable
    Tuesday, November 25, 2008 10:01 PM

Answers

  • LINQ to SQL will take the value from each column in the result set and try and assign it to instances of your type, PersonDTO. NULL is a value just like 1, 2 and 3, etc - it is just a value with special meaning and LINQ to SQL doesn't attempt to infer anything from that meaning. System.Int32, like any other value type, doesn't have a value that maps to NULL. You need a Nullable<> wrapper around your value type properties. To do this locate the property or properties for PersonDTO in the DBML file and ensure they are set to 'Nullable' in the properties window.

     

    You may wish to read the following article first: http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx.  

    Thursday, November 27, 2008 2:28 PM
    Answerer

All replies

  • LINQ to SQL will take the value from each column in the result set and try and assign it to instances of your type, PersonDTO. NULL is a value just like 1, 2 and 3, etc - it is just a value with special meaning and LINQ to SQL doesn't attempt to infer anything from that meaning. System.Int32, like any other value type, doesn't have a value that maps to NULL. You need a Nullable<> wrapper around your value type properties. To do this locate the property or properties for PersonDTO in the DBML file and ensure they are set to 'Nullable' in the properties window.

     

    You may wish to read the following article first: http://msdn.microsoft.com/en-us/library/1t3y8s4s(VS.80).aspx.  

    Thursday, November 27, 2008 2:28 PM
    Answerer
  • Big thx. Well actually my PersonDTO is not a class generated by the ling-to-sql-Designer. It is a custom class consisting only o fproperties and constants combining 2 linq-to-sql created entities.

    So PersonDTO combines the linq to sql entities Person and Person_Details.

    This works although it is written "
    If <T> is an entity explicitly tracked by the DataContext. "

    It also works if person has a c# system.nullable.

    My mistake was that i one of the properties in PersonDTO had a int as a datatype instead of a short, and the db datatype was small int.

    I wish the exception would include which property threw the exception. So with large DTOs it is very annyoing to pass a test sql statement!


    Friday, November 28, 2008 7:12 PM