如何在linqpad中执行左连接 -

时间:2012-05-03 16:55:26

标签: linq-to-sql left-join linqpad

如何获得linq pad来运行我的左连接,如下所示?

var query = 
            from s in db.CDBLogsHeaders
                    .OrderByDescending(g => g.LogDateTime)
                    from sc in db.StyleColors
                        .Where(stylecolor => stylecolor.id == (int?)s.StyleColorID)
                        .DefaultIfEmpty()
                    from c in db.StyleHeaders
                        .Where(styleHeader => styleHeader.id == (int?)s.StyleHeaderID)
                        .DefaultIfEmpty()
        select new 
        {
            CDBLogsHeaderId = s.Id,
            Merchandiser = c.Merchandiser,
            Material = s.Material,
            Season = s.Season,
            LogsHeaderLogType = s.LogType,
            PushFromTo = s.PushFromTo,
            LinePlan = s.LinePlan,
            QuikRefNumber = s.QuikRefNumber,
            PLMOrigin = s.PLM_Origin,
            SeasonOriginal = c.SeasonOriginal,
            SeasonCurrent = c.SeasonCurrent,
            StyleHeaderId = c.Id,
            StyleCode = c.StyleCode,
            StyleColorsColorCode = sc.ColorCode
        };

    query.Dump();

linq pad创建的sql在Management Studio中运行完美,但linq-pad不显示任何行并给出此错误

  

InvalidOperationException:无法将null值分配给a   System.Int32类型的成员,它是一个不可为空的值类型。

如何让linqpad工作,以便我可以玩它?

1 个答案:

答案 0 :(得分:3)

在您的匿名类型中,请确保您的整数正在返回整数。

更改

StyleHeaderId = c.Id,

StyleHeaderId = (c.Id == null ? 0 : c.Id),