none
Problem Linq-to-SQL Left Join query RRS feed

  • Question

  • Hi,

    I tried to get a left join result with linq to sql like this:

    Dim result = _
        From parent In dc.Parents _
        Group Join child In dc.Children _
        On parent.ParentId Equals child.FkParentId 
        Into grp = Group
        From o In grp.DefaultIfEmpty _
        Select New With { _
            .parentId = parent.ParentId _
            , .childId = o.ChildId _
            }
    Dim l = result.ToList
    

    All the Id fields are declared as Int32 values.

    I get the following error in the line where the query is executed by the result.ToList command if there exists one parent without children:

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

    Does anybody have a solution?
    Thanks in advance,
    Alex

    Friday, July 6, 2012 8:33 AM

Answers

  • Because the child come with null value. So you may set childId in Select New as nullable this way

        Select New With { _
            .parentId = parent.ParentId _
            , .childId = CType(o.ChildId, Integer?) _
            }

    Cheer!

    • Marked as answer by webjagger Friday, July 6, 2012 10:41 AM
    Friday, July 6, 2012 9:48 AM