需要解决空问题的问题

发布于 2024-08-16 05:54:07 字数 1141 浏览 2 评论 0原文

现在一天了,我在我的存储库中遇到了这个空问题。这是我为 linq to sql 编写的代码...我尝试了很多选项,但对此没有帮助。

这里的问题是,如果 vidList 为空值,它就会卡在第三行。

如果vidList没问题,但fidListE为null,返回时仍然会导致null异常。

我尝试了很多选项,例如使用 Count、使用 '??' ...但仍然没有帮助。

    public List<ATTACHMENT> existedAttachment(IList<int> vidList, IList<int> fidList, IList<int> iidList)
    {
        IEnumerable<int> vidListE =  vidList.Distinct();
        IEnumerable<int> fidListE = (fidList != null) ? fidList.Distinct() : null;
        IEnumerable<int> iidListE = (iidList != null) ? iidList.Distinct() : null;
        return (from d in _db.ATTACHMENTs
                   .Where<ATTACHMENT>(d =>
                       ((vidListE != null) ? (vidListE.Contains<int>(d.VID_ID.Value)) : false) ||
                       ((fidListE != null) ? (fidListE.Contains<int>(d.FID.Value))    : false) ||
                       ((iidListE != null) ? (iidListE.Contains<int>(d.IMG_ID.Value)) : false)
                    )
                select d).ToList<ATTACHMENT>();
    }

有人可以给我提供一点线索吗?非常感谢。我的脑子里全是新年。 :P

For a day now i'm stuck with this null problem in my repository. here is my piece of code writen for linq to sql... i've tried a lot of options but no help for this.

the problem here is if the vidList got null value, it got stuck right in 3rd line.

if the vidList is ok, but the fidListE got null, it will stil cause null exception in the return.

I tried quite a lot of options like use Count, use '??' ... but still no help.

    public List<ATTACHMENT> existedAttachment(IList<int> vidList, IList<int> fidList, IList<int> iidList)
    {
        IEnumerable<int> vidListE =  vidList.Distinct();
        IEnumerable<int> fidListE = (fidList != null) ? fidList.Distinct() : null;
        IEnumerable<int> iidListE = (iidList != null) ? iidList.Distinct() : null;
        return (from d in _db.ATTACHMENTs
                   .Where<ATTACHMENT>(d =>
                       ((vidListE != null) ? (vidListE.Contains<int>(d.VID_ID.Value)) : false) ||
                       ((fidListE != null) ? (fidListE.Contains<int>(d.FID.Value))    : false) ||
                       ((iidListE != null) ? (iidListE.Contains<int>(d.IMG_ID.Value)) : false)
                    )
                select d).ToList<ATTACHMENT>();
    }

can some one please provide me a little clue. Thank you very much. my brain just stuck with newyear. :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

记忆里有你的影子 2024-08-23 05:54:07

不要使用 null,而是使用内置的 Empty 枚举:

IEnumerable<int> fidListE = (fidList != null) ? fidList.Distinct() : Enumerable.Empty<int>();

Contains() 方法现在始终返回 false,并且您不必在查询中检查 null。

Instead of using null, use the built-in Empty enumerable:

IEnumerable<int> fidListE = (fidList != null) ? fidList.Distinct() : Enumerable.Empty<int>();

The Contains() method now always returns false and you don't have to check for null in the query.

音栖息无 2024-08-23 05:54:07

您是否在该方法的一开始就尝试过类似的操作?如果参数为空,则将其设置为空列表。

 IEnumerable<int> vidListE = (vidList != null) ? vidList.Distinct() : new List<int>();
 IEnumerable<int> fidListE = (fidList != null) ? fidList.Distinct() : new List<int>();
 IEnumerable<int> iidListE = (iidList != null) ? iidList.Distinct() : new List<int>();

return (from d in _db.ATTACHMENTs
                   .Where<ATTACHMENT>( d =>
                       vidListE.Contains<int>(d.VID_ID.Value) ||
                       fidListE.Contains<int>(d.FID.Value) ||
                       iidListE.Contains<int>(d.IMG_ID.Value) )
                    )
                select d).ToList<ATTACHMENT>();

Have you tried something like this at the very beginning of the method. This sets it to an empty list if the parameter is null.

 IEnumerable<int> vidListE = (vidList != null) ? vidList.Distinct() : new List<int>();
 IEnumerable<int> fidListE = (fidList != null) ? fidList.Distinct() : new List<int>();
 IEnumerable<int> iidListE = (iidList != null) ? iidList.Distinct() : new List<int>();

return (from d in _db.ATTACHMENTs
                   .Where<ATTACHMENT>( d =>
                       vidListE.Contains<int>(d.VID_ID.Value) ||
                       fidListE.Contains<int>(d.FID.Value) ||
                       iidListE.Contains<int>(d.IMG_ID.Value) )
                    )
                select d).ToList<ATTACHMENT>();
浅唱々樱花落 2024-08-23 05:54:07

列表是否不应由可空整数组成,即

Should the list not be made up of nullable ints i.e

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文