c# left join 与几个表

c# left join whith several tables

我有两个table

Table A(2 个字段。CODART 和 DES) Table B(2 个字段。CODART 和 PREVEN)

我需要使用 2 table 和 return json 进行左连接。 table B 当没有记录时出现此错误。

((( 转换为值类型 'System.Decimal' 失败,因为具体化值为 null。结果类型的泛型参数或查询必须使用可空类型。)))

我用这个代码

        var lstart = (from art in db.tableA
                      where art.CODART == 'uno'
                      join tfa in db.tableB on art.CODART equals tfa.CODART into ArtTfa from AA in ArtTfa.DefaultIfEmpty()
                      select new 
                      {
                          art.CODART,
                          art.DES,
                          AA.PREVEN
                      });

        return Json(new { lista = lstart }, JsonRequestBehavior.AllowGet);

你知道为什么吗? 谢谢

对不起,我的问题。我找到了解决方案。

是否需要在字段前加上(十进制?)。

新的代码

        var lstart = (from art in db.tableA
                      where art.CODART == 'uno'
                      join tfa in db.tableB on art.CODART equals tfa.CODART into ArtTfa from AA in ArtTfa.DefaultIfEmpty()
                      select new 
                      {
                          art.CODART,
                          art.DES,
                          (Decimal?)AA.PREVEN
                      });

        return Json(new { lista = lstart }, JsonRequestBehavior.AllowGet);

非常感谢。