需要解决空问题的问题
现在一天了,我在我的存储库中遇到了这个空问题。这是我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要使用 null,而是使用内置的 Empty 枚举:
Contains() 方法现在始终返回 false,并且您不必在查询中检查 null。
Instead of using null, use the built-in Empty enumerable:
The Contains() method now always returns false and you don't have to check for null in the query.
您是否在该方法的一开始就尝试过类似的操作?如果参数为空,则将其设置为空列表。
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.
列表是否不应由可空整数组成,即
Should the list not be made up of nullable ints i.e