int? and int comparison when LEFT OUTER JOIN in Linq issue

前端 未结 2 1255
被撕碎了的回忆
被撕碎了的回忆 2020-12-22 10:53

I am working on a WinForms project which requires to use Linq-To-Sql. I have been able to create my DataContext using the SqlMetal tool, and make s

2条回答
  •  暖寄归人
    2020-12-22 11:13

    The first problem was that the property names should be the same as said casperOne♦, but the second problem is that you're comparing a nullable-int, p.child_ID, with a non-nullable-int, t.ID. So you could use the null-coalescing operator in this way:

    (int)(p.child_ID ?? default(int))
    

    In this case returns p.child_ID if it isn't null else returns default(int), that is 0.

    The query will be:

    var query = from p in db.ParentTable
                join t in db.ChildTable on new {A = (int)(p.child_ID ?? default(T)), B = p.OtherID}
                equals new {A = t.ID, B = t.OtherID} into j1
                from c in j1.DefaultIfEmpty()
                select new
                {
                   //...
                };
    

提交回复
热议问题