4

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.

4
  • 2
    You haven't shown us enough to tell really. What's the "ID" here? Please provide a short but complete program to demonstrate the problem. I suspect as soon as we can reproduce it we'll find the issue easily. I would suggest that having "seg" and "segs" is a recipe for confusion. Commented Aug 21, 2009 at 18:41
  • Are you certain that the Line.Segments collection is correct? Commented Aug 21, 2009 at 18:42
  • Please post a complete, but short, program that can be compiled and executed to reproduce your problem. This will help us help you by giving us code that we can debug and tweak in order to get the desired results. Commented Aug 21, 2009 at 18:43
  • I found why it was not returning what I expected. ModType is NULLABLE, therefore, this snippet of code should be like this... [code] return seg => seg.ModType == Deleted && seg.Line.Segments.Any(segs => segs.ID != seg.ID && (segs.ModType != Deleted || segs.ModType == null)); [code] Thanks for all the comments! Commented Aug 21, 2009 at 19:46

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Any returns a bool, not a collection.
This will return a Lambda expression that will be used in a query.
0

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

Comments

0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.