如何在 linq to sql 中选择全部
在我的 LINQ 查询中,如果值为 null
,我尝试选择全部:
var filteredLesson = (from l in storeDB.lessons
where l.statusID == SUBMITTED ||
l.statusID == APPROVED &&
l.projectID == (l.projectID.HasValue
? lesson.projectID : /*select all*/ )
select l).ToList();
有办法做到这一点吗?
In my LINQ query, I am trying to select all if a value is null
:
var filteredLesson = (from l in storeDB.lessons
where l.statusID == SUBMITTED ||
l.statusID == APPROVED &&
l.projectID == (l.projectID.HasValue
? lesson.projectID : /*select all*/ )
select l).ToList();
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果值为
null
,您只想将其与自身进行比较。这样,如果值为null
,您的行将包含在结果中。You just want to compare it to itself if the value is
null
. That way your row will be included in the results if the value isnull
.也许是这样的?
Something like this perhaps?
在不知道您的对象模型的情况下,很难完全确定这一点,但我想您应该寻找类似以下的内容:
Without knowing your object model it's hard to be totally sure about this, but I imagine something like the following should be what you're looking for:
以下是使用 xUnit.Net 测试编写的上述许多答案。请注意,原作者在他选择的答案中的评论(这是一个错误的解决方案)给出了他期望的结果。我清理了他的解决方案以删除重复的比较。
Here's are many of the above answers written up using xUnit.Net tests. Note that the original author's comment in the answer he selected (which is an incorrect solution) gives the results he expected. I cleaned up his solution to remove the duplicate comparisons.