views:

248

answers:

3

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.

+2  A: 

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
Bryan Rowe
`Any` returns a `bool`, not a collection.
Jon Skeet
Updated, thanks Jon.
Bryan Rowe
This will return a Lambda expression that will be used in a query.
Jaime
A: 

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

virsum
A: 

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!

Jaime