-1

I have a .Any() Linq Method:

db.MyTable.Any(x => x.Year == MyObj.Year && x.Quarter == MyObj.Quarter && x.Week == MyObj.Week)

That is returning the error:

 System.InvalidOperationException occurred
  HResult=0x80131509
  Message=Sequence contains no matching element
  Source=EntityFramework

However the MSDN documentation states that the .Any method returns "true if the source sequence contains any elements; otherwise, false."

Why is this method throwing the exception instead of returning False?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
user3839756
  • 793
  • 1
  • 9
  • 22
  • That's because `db.MyTable` is empty. – Lasse V. Karlsen Dec 15 '17 at 18:47
  • The documentation says that for the "parameterless" version of `Any`; it says something different for the version that takes a predicate. – Kenneth K. Dec 15 '17 at 18:53
  • @LasseVågsætherKarlsen This exception does throw if the table is empty, however my table is not empty. – user3839756 Dec 15 '17 at 18:56
  • @KennethK. [That version says the same thing.](https://msdn.microsoft.com/en-us/library/bb534338(v=vs.110).aspx) – Joel Coehoorn Dec 15 '17 at 18:56
  • @JoelCoehoorn No, it does not. – Kenneth K. Dec 15 '17 at 19:04
  • @KennethK. Did you check the link in my comment? – Joel Coehoorn Dec 15 '17 at 19:05
  • @JoelCoehoorn Yes. How do you consider `true if any elements in the source sequence pass the test in the specified predicate; otherwise, false` and `true if the source sequence contains any elements; otherwise, false` to be the same statement? – Kenneth K. Dec 15 '17 at 19:06
  • Can you post stack trace of that exception? – Evk Dec 15 '17 at 19:16
  • Perhaps the error isn't in that part of your expression, since that isn't the complete code? – NetMage Dec 15 '17 at 23:54
  • check your mappings, most likely there is some mismatch in there... see also https://stackoverflow.com/questions/22982749/sequence-contains-no-matching-element-entityframework – Dan Dohotaru Dec 19 '17 at 07:34
  • `Any` itself *never* throws this exception. We need to see the stack trace. Since it's EF, I bet it's some flaw in the mapping model --emerging while translating the expression into SQL-- rather than anything else. – Gert Arnold Oct 11 '22 at 13:38

2 Answers2

0

With this little code it is fairly difficult to see what the cause is. It certainly is not in this part of the code.

Try to do some debugging, use the debugger to check all values, or write some Debugging statements before you perform your function:

// Check myObj
MyObjectType myObj = ...         // instead of var, so you are certain you have the right type
Debug.Assert(myObj != null);

// only if Year, Quarter, Week are classes:
Debug.Assert(myObj.Year != null);   
Debug.Assert(myObj.Quarter != null);
Debug.Assert(myObj.Week != null);

// check your db:
Debug.Assert(db != null);
Debug.Assert(db.MyTable != null);
int count1 = db.MyTable.Count();
int count2 = db.MyTable
    .Where(x => x.Year == MyObj.Year 
        && x.Quarter == MyObj.Quarter
        && x.Week == MyObj.Week)
    .Count();
bool hasAny = db.MyTable
    .Where(x => x.Year == MyObj.Year 
        && x.Quarter == MyObj.Quarter
        && x.Week == MyObj.Week)
    .Any();

// if all this works, your statement should also work
hasAny = db.MyTable
    .Any(x => x.Year == MyObj.Year 
        && x.Quarter == MyObj.Quarter
        && x.Week == MyObj.Week);
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
-1

Is hard to see, with your example but probably you must check if you have Null values in "MyTable" and the datatypes of MyTable.Year,MyTable.Quarter and MyTable.Week match with the sames in MyObj...

  • The data types do match (The model was built with code first from db). The DB does not have any null values for the fields being queried. – user3839756 Dec 15 '17 at 18:49