C# Lambda 表达式未返回预期结果

发布于 2024-08-03 00:51:28 字数 787 浏览 5 评论 0原文

我正在使用 lamda 表达式来过滤查询。

基本上,我的行由段组成,这些段被标记为已删除、已插入或为空。

我想要返回的是已标记为已删除但其任何同级未标记为已删除的段。例如,

Line: "Soylent Green is people!" Broken into 2 segments... 
Segment 1: "Soylent Green " (marked as deleted)
Segment 2: "is people!" (not marked as deleted)

应该返回段 1。但下一个示例,

Line: "Open the pod bay doors Hal!" Broken into 3 segments...
Segment 1: "Open the " (marked as deleted)
Segment 2: "pod bay " (marked as deleted)
Segment 3: "doors Hal!" (marked as deleted)

不应返回任何段。请参阅下面的代码:

return seg => seg.ModType == Deleted &&
              seg.Line.Segments.Any(segs => segs.ID != seg.ID && 
              segs.ModType != Deleted);

谢谢,我很感谢任何关于为什么这不起作用的帮助或建议。

I am using a lamda expression to filter a query.

Basically, I have lines that are composed of segments and these segments are marked as deleted, inserted or null.

What I want returned are segments that have been marked as deleted but whose any sibling IS NOT marked as deleted. As an example,

Line: "Soylent Green is people!" Broken into 2 segments... 
Segment 1: "Soylent Green " (marked as deleted)
Segment 2: "is people!" (not marked as deleted)

Should return me Segment 1. But the next example,

Line: "Open the pod bay doors Hal!" Broken into 3 segments...
Segment 1: "Open the " (marked as deleted)
Segment 2: "pod bay " (marked as deleted)
Segment 3: "doors Hal!" (marked as deleted)

Should not return any segments. See code below:

return seg => seg.ModType == Deleted &&
              seg.Line.Segments.Any(segs => segs.ID != seg.ID && 
              segs.ModType != Deleted);

Thanks and I appreciate any help or suggestion as to why this is not working.

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

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

发布评论

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

评论(3

葬花如无物 2024-08-10 00:51:28

我想它会是这样的:

return s =>  from segment in s.Line.Segments 
             where segment.ModType == Deleted 
                && segment.Line.Segments.Any(segs => segs.ModType != Deleted)
             select segment

I'd think it would be something like:

return s =>  from segment in s.Line.Segments 
             where segment.ModType == Deleted 
                && segment.Line.Segments.Any(segs => segs.ModType != Deleted)
             select segment
や莫失莫忘 2024-08-10 00:51:28

使用Where()而不是Any(),它将返回一个IEnumerable对象。正如 Jon 所说,Any() 返回一个布尔值。

Instead of Any(), use Where(), which will return you an IEnumerable object. Like Jon says, Any() returns a boolean.

疯到世界奔溃 2024-08-10 00:51:28

由于 ModType 可为空,因此它没有返回我所期望的内容。代码应该是...

return seg => seg.ModType == Deleted &&
          seg.Line.Segments.Any(segs => segs.ID != seg.ID && 
          (segs.ModType != Deleted || segs.ModType == null));

感谢所有回复并提出意见和建议的人!

Since ModType is nullable, it did not return what I was expecting. The code should be...

return seg => seg.ModType == Deleted &&
          seg.Line.Segments.Any(segs => segs.ID != seg.ID && 
          (segs.ModType != Deleted || segs.ModType == null));

Thanks to all those who replied and gave comments and suggestions!

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