0

I have the following linq query:

    var test = vendorContact.vendorContactItem
                     .Where(x => x.ItemNumber == vendorContactItem.Item_Number)
                     .FirstOrDefault();

It fails on this piece of code, "Value cannot be null, parameter name: source" ... yet it also displays, in the local variables window, "test" as a variable with all its properties populated.

vendorContact.VendorContactItem is null. Presumably this would be the first element to be added to the list. So how is "test" evaluating correctly while simultaneously throwing up that error?

I'm new to Linq, so excuse me if this is an obvious question.

4
  • vendorContact.VendorContactItem cannot be both null and an empty list. Which is it? Commented Dec 1, 2010 at 21:34
  • "null" and "a list with no elements" are two different things. Commented Dec 1, 2010 at 21:34
  • The LINQ extension methods (all extension methods) are invoked as: fn(obj, p1, p2, ...). That is, they are not invoked upon a receiver (they are static methods) and thus can react to null (e.g. when obj == null) how they choose to. Usually throwing an ArgumentNullException is appropriate. Not sure if this answers/explains your question though. Commented Dec 1, 2010 at 21:35
  • Good catch guys, you are correct it is not an empty list, it is actually null, I have edited the question to reflect this. Commented Dec 1, 2010 at 21:37

1 Answer 1

1

If this is in a loop test in the locals window contains the last value of test, from the last iteration of the loop.

Edit: This has really nothing to do with LINQ, but how the debugger works.

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

1 Comment

Ah ha! Well, I did say the answer might be obvious. Thanks a bunch!

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.