This is a migrated thread and some comments may be shown as answers.

Query execution did not find a matching element

12 Answers 375 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Duncan
Top achievements
Rank 2
Duncan asked on 17 Oct 2011, 07:09 PM
I am getting this error: Query execution did not find a matching element. Yet i know for a fact that there is a matching value in the DB.

Also the query does work most of the time, and especially if there is a breakpoint in the code somewhere. If i let the code run freely, this is when the error is produced.

The particular line of code is:

...
trig.States = dbContext.States.Single(s => s.StateCode == trigger.VisitorState.StateCode);
...
dbContext.Add(trig);
}
...
dbContext.SaveChanges();

Does anyone know why this is?

Duncan

12 Answers, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 18 Oct 2011, 09:36 AM
Hello Duncan,

please give us a bit more information:
(a) which CLR type is StateCode
(b) what column type does StateCode have
(c) is StateCode the primary key
(d) can there be a upper/lower case issue?

Best wishes,
Thomas
the Telerik team

Check out the latest stable build of Telerik OpenAccess ORM. Download it and benefit from our new Project Templates.

0
Duncan
Top achievements
Rank 2
answered on 18 Oct 2011, 05:07 PM
a: String

b: nvarchar(50) - However, i have changed this to nvarchar(MAX) as i was noticing some blank characters that needed to be trimed

c: No, it is (most of the time) 2 characted code like CA, AZ, FL, etc...

d: No, and even the blank spaces should nto have been a issue as i was doing a lookup for the value out of the DB and then later this exact value out of the DB was being used in this query, so blank spaces or case should have been exact to what was in the DB...
0
Thomas
Telerik team
answered on 19 Oct 2011, 10:14 AM
Hi Duncan,

ok, that means we have a situation where the query is executed against the database (no pk shortcut taken) and sometimes fails to find an instance that seems to have been there before. Now, I have more questions:
(a) Which database do you use.
(b) Please check that the column type in the model for State is correctly set to nvarchar(max).
(c) I would really like to understand why debugging should have an influence on the result. The only thing that I think could play a role here is the automatic display of property values in the debugger, as the display will need to fetch them from the database, thus altering the internal state compared to non-debugging executions. Can you turn off this feature in the Visual Studio (tools/debugging) and try again?

Kind regards,
Thomas
the Telerik team

Check out the latest stable build of Telerik OpenAccess ORM. Download it and benefit from our new Project Templates.

0
Caleb
Top achievements
Rank 1
answered on 08 Nov 2011, 02:32 PM
I am getting this same error.
"Query execution did not find a matching element."  I have called

if (scope.Transaction.IsActive == false)
        {
            scope.Transaction.Begin();
            Genotype genotype = (from g in scope.Extent<Genotype>() where g.IdGenotype == id select g).First();

}

I am getting the error on the bold line and I can not see what could be wrong
0
Thomas
Telerik team
answered on 08 Nov 2011, 03:17 PM
Hi Caleb,

this exception occurs, when there is no instance with that id. In case you want to get a null back instead of the exception, you will need to use FirstOrDefault().

Greetings,
Thomas
the Telerik team

NEW and UPDATED OpenAccess ORM Resources. Check them out!

0
Caleb
Top achievements
Rank 1
answered on 08 Nov 2011, 03:22 PM
Thank you Tom fro your response it has been very helpful but I know that record exists....  What other issues could exist?  
0
Thomas
Telerik team
answered on 08 Nov 2011, 08:17 PM
Hello Caleb,

is the instance locked on the database, are there parallel transactions running? What is the mapping of the field and what is the field type?

Best wishes,
Thomas
the Telerik team

NEW and UPDATED OpenAccess ORM Resources. Check them out!

0
Duncan
Top achievements
Rank 2
answered on 08 Nov 2011, 11:27 PM
Caleb,

My issue was a difference in the database column vs entity type. I can't recall the exact types unfortunatly but it was something silly like my DB column was nchar(10) and after switching it to nvarchar(MAX) the problem seemed to go away.

I can dig into this further for you if that does not help.

Duncan
0
Thomas
Telerik team
answered on 10 Nov 2011, 04:43 PM
Hello Caleb,

is this a nvarchar/varchar issue? Again, can you give us this information; we would like to rule this out as a problem before releasing a new version.

Regards,
Thomas
the Telerik team

NEW and UPDATED OpenAccess ORM Resources. Check them out!

0
Caleb
Top achievements
Rank 1
answered on 10 Nov 2011, 06:35 PM
Well guys you where right with the first issue.  Come to find out the query was not the same as the class and DB.

CD
0
Thomas
Telerik team
answered on 11 Nov 2011, 09:51 AM
Hello Caleb,

so the issue is resolved for you now? And the reason was that the class/table information did not match what you had in the database? Was there a manual change made by you to the mapping or did we read the database tables incorrecty? If the latter is the case, what kind of table definition did you have?

All the best,
Thomas
the Telerik team

NEW and UPDATED OpenAccess ORM Resources. Check them out!

0
Ian
Top achievements
Rank 1
answered on 24 Apr 2012, 01:38 AM
I think the paragraph
"
My issue was a difference in the database column vs entity type. I can't recall the exact types unfortunatly but it was something silly like my DB column was nchar(10) and after switching it to nvarchar(MAX) the problem seemed to go away.
"
points out the issue.  The column was a fixed width column - nchar(10).  So even if it was only fed 3 characters "ABC" there would still be "ABC......." (dot for space) in the column.  A LIKE/StartsWith query would have worked, or using a varchar/nvarchar column would also have worked. 
I tend to avoid the fixed width columns for this very reason.  I often have to deal with it when we read data into our SQL database from FoxPro and other DB systems.  Storing the data in varchar columns and using RTRIM() makes things tidy.
Tags
LINQ (LINQ specific questions)
Asked by
Duncan
Top achievements
Rank 2
Answers by
Thomas
Telerik team
Duncan
Top achievements
Rank 2
Caleb
Top achievements
Rank 1
Ian
Top achievements
Rank 1
Share this question
or