LINQ左外连接NullReferenceException

时间:2017-05-16 01:28:44

标签: linq

进行左外连接时,我得到NullReferenceException。

类似的查询在LINQ Pad中有效但在Visual Studio 2015中无效。用户代码未处理System.NullReferenceException   的HResult = -2147467261   Message =对象引用未设置为对象的实例。

提前致谢。

.Net Core LINQ Code不起作用:

    public IEnumerable<R> GetAll()
    {
        var results =
            from a in uow.R.GetAll()
            join b in uow.SSR.GetAll() on a.ID equals b.RID into g9
            from c in g9.DefaultIfEmpty()
            join d in uow.SS.GetAll() on c.SSID equals d.ID into g10
            from e in g10.DefaultIfEmpty()
            select new
            {
                a.Id,
                SSID = c == null ? null : (int?)c.SSID,
                e.Name
            };
        List<R> rList = new List<R>();
        foreach (var a in results)
        {
            resourceList.Add(new R { ID = a.Id, SSID = a.SSID });
        }
        return rList.ToArray<R>();
    }
`
    var Result =    
    from a in Tbl_R             
    join b in Tbl_SS on a.ID equals b.RID into g9
    from c in g9.DefaultIfEmpty()           
    join d in Lu_SS on c.SSID equals d.SSID into g10
    from e in g10.DefaultIfEmpty()
    select new
    {
        a.ID,
        SSID = c == null ? null : (int?)c.SSID,
        e.Name
    };
    Result.Dump();


SELECT t2.ID,
ISNULL(t17.SSID,'') as SSID
ISNULL(t17.Name,'') as Name
FROM R t2  
LEFT JOIN SSR t16 ON t16.RID=t2.ID
LEFT JOIN SS t17 ON t16.SSID=t17.SSID

0 个答案:

没有答案