1

我正在尝试从 Fderive 中获取所有数据,但是我正在尝试使用 where 子句设置过滤器。不幸的是,当我在 spd 中的一行为空时触摸 spd 时,我得到了一个 nullreferencexpection。

var Result = from fpd in FDerive
                             join spd in SDerive
                             on new { fpd.PId, fpd.SId }
                             equals new { spd.PId, spd.SId } into allRows
                             from spd in allRows.DefaultIfEmpty()
                             where spd.SId == ""
                             || spd.PId == ""
                             select new { fpd, spd };

如何解决空错误?

4

2 回答 2

0

我在以下问题的代码底部找到了答案

LINQ 双左连接

var results =
        from person in students
        join entry in subquery on person.FullName equals entry.AuthorFullName into personEntries
        from personEntry in personEntries.DefaultIfEmpty()
        orderby person.FullName
        select new
        {
            PersonName = person.FullName,
            BlogTitle = personEntry == null ? "" : personEntry.Title
        };
于 2013-03-31T22:32:50.000 回答
0

DefaultIfEmpty<T>如果源集合为空,将返回一个仅包含一个默认值为T- 在这种情况下为 - 的元素的集合。如果连接中没有返回任何项目,也会null如此。spdnull

尝试null检查 where 子句

var Result = 
    ...
    where spd == null || spd.SId == "" || spd.PId == ""
    select new { fpd, spd };
于 2013-03-31T17:01:02.510 回答