2

I have this lambda expression:

if (!myList.Any( x => x.name == "example" && x.articleNumber == "1"))
{

}
else
{

}

myList contains an object from the class which has these properties: articleNumber, name and quantity.

And this enters in the if instead of in the else althought there is an object with name "example" AND articleNumber "1". Why does this happen?

12
  • 1
    What are the contents of myList? Commented Jan 4, 2013 at 9:27
  • if you don't have an entry with name example and acticleNumber 1 it should go into the if block. Commented Jan 4, 2013 at 9:28
  • Because no elements of myList match the predicate? If you think something should match, I'd check again. (The debugger will be your friend here and you can iterate through the list if you'd like) Commented Jan 4, 2013 at 9:28
  • What is in the list and what is your expected result? We can't tell you why without the scope. Commented Jan 4, 2013 at 9:29
  • Sorry, I edited the question. Commented Jan 4, 2013 at 9:29

6 Answers 6

4

You are aware of the ! before your lamda right?

what you are asking in the if statement is

If myList does NOT contain any values where name is example AND article number is 1

I don't know if this helps but maybe it's easier to answer if you supply more information about what you want to accomplish with the statement.

Sign up to request clarification or add additional context in comments.

Comments

1

The only reason i can see is that there's no element with name=="example" and articleNumber=="1". Note that the == operator is case sensitive in C# and that there might be a white-space somewhere.

Then you can use this overload of Equals to compare case-insensitive and remove white-spaces with Trim:

if (!myList.Any( x => x.name.Trim().Equals("example", StringComparison.OrdinalIgnoreCase) 
                   && x.articleNumber == "1"))
{

}
else
{

}

You should also consider to change the type of articleNumber to int since it is a number.

Comments

1

I'm pretty sure that the list don't contain the entry you think it does. Set a debug marker on the if line, and inspect the list at that point to see what it contains. You will find that the entry is not there. Remember that strings are case sensitive as well, so if name really is Example, it will not match.

3 Comments

It does not contain exactly that at least. Im sure that Any is not broken in suck a way.
So the most likely cancidates for it looks the same is a problem with case in the text "example", or some whitespace somewhere in one of the two strings.
If you still believe it contains the correct value, take a screenshot of the inspected list when debugging at the if statement, and post it to your question.
1

Nothing wrong with your logic but you counld try??

if (myList.Where( x => x.name == "example").Where(x=> x.articleNumber == "1").Count() > 0)
{

}
else
{

}

OR to remove all spaces and variances in Casing

if (!myList.Any( x => x.name.Trim().ToLower() == "example" && x.articleNumber.Trim().ToLower() == "1"))
{

}
else
{

}

2 Comments

Why would you complicate the if statement to produce the exact same result?
@ØyvindBråthen - he added a .Trim() and a .ToLower() to eliminate cases such as "Example", "example ", etc.
1

Your list probably contains some values that you don't expect.

Try to replace && with || and see what you will get:

var v = myList.Where(x => x.name == "example" || x.articleNumber == "1").ToList();

just to see what's there .

Comments

0

The definition of Any: Determines whether any element of a sequence exists or satisfies a condition.

So for each element in a sequence it is determined if the predicate is matching your expression.

myList.Any( x => x.name == "example" && x.articleNumber == "1")

Will return true if ANY element of the List has the name "example" AND the articleNumber 1.

!myList.Any( x => x.name == "example" && x.articleNumber == "1")

Will return true if NO element of the List has the name "example" AND the articleNumber 1.

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.