Last post Feb 08, 2016 08:15 PM by JWhitmire
Feb 19, 2014 05:14 PM|AndyW2009|LINK
I have a LINQ query var record = dc.sometabel.Where( u => u.username == "someusername").SingleOrDefault() if a matching record is not found I would expect that the variable record would equate to null (this being its default).
However, the query is returning the first record in the database. Using the SQL Profiler I can see the query submitted is SELECT t0.username, t0.somefield FROM dbo.sometable AS t0.
The query is missing the WHERE clause.
Does anyone know why the where clause part of the query would not be included in the query.
VS2013, SQL Server 2012 .Net 4.5 (I note that on my dev rig and test rig the C# compiler versions are different (4.0.30319.18408 for my dev rig where I dont get the problem and 4.0.30319.33440 where I do get the problem).
Update: There is only one record in the table at the moment and I am testing the scenario where an incorrect username is entered, so I am expecting to get no data back.
Update 2: After some more testing I have found that it is occuring with any query written in the form dc.table.where( x => x.y == z). If I re-write the query as (from x in table where x.y == z).singleordefault() it works fine.
Feb 20, 2014 10:05 PM|Terry Guo - MSFT|LINK
Do you have override the Where clause?
If so, please post your override code here.
You can also using the Equals function instead of "==", like this:
var record = dc.sometabel.Where( u => u.username.Equals("someusername")).SingleOrDefault()
Hope it helps.
Feb 24, 2014 06:48 PM|AndyW2009|LINK
no override in the where clause. No matter what I try (including .equals) teh where clause is not generated and sent through to SQL. Only usnig the old notation (from x in dc.y etc) do I get a generated where clause.
Feb 25, 2014 02:04 AM|Terry Guo - MSFT|LINK
I don't know the reason, you can try to use the FirstOrDefault() to instead of SingleOrDefault().
If it also work, I suggest you migration your .net framwork to lastest version.
Mar 11, 2014 07:09 PM|AndyW2009|LINK
Hi, none of the extensions work - I have changed all of my queries to use (from x in dc.abc where efg, select x).singleOrDefault() and they work. if I use the other notation, nothing works. I am running the latest version of .net and VS development
environment (Vs2013 update 1).
I have the suspicion it has been broken in one of the latest Microsoft updates as this code used to work fine.
Mar 15, 2014 11:26 AM|jasminder.33|LINK
There is another extension method FirstOrDefault, which is different from
SingleOrDefault. What you are looking for, will most probably be achieveable by using FirstOrDefault and not singleOrDefault.
Try the link below :
Hope it helps...!!!
Mar 15, 2014 01:04 PM|Rion Williams|LINK
I haven't experienced any issues regarding FirstOrDefault() or SingleOrDefault() and their usage will really depend on if you expect your collection that is being queried to contain just a single record (in which each of these will function the same)
or if you are going to expect multiple (in which case FirstOrDefault will return the first value).
Personally, I am a big fan of the method syntax as I find it is a bit easier to read and would look like :
var query = dc.abc.FirstOrDefault(x => x.efc);
var query = dc.abc.SingleOrDefault(x => x.efc);
Jul 07, 2014 04:12 PM|luciddev|LINK
You may be missing a using statement -> using System.Linq
Feb 08, 2016 08:15 PM|JWhitmire|LINK
Very similar issue:
var daRow = dataset.table.FirstOrDefault(x => x.Host == this.host)
This is returning the first (and only) record in the table even though it doesn't satisfy the predicate. Same thing happens with SingleOrDefault. Had to revert to the old-fashioned foreach loop to correctly get the null value for daRow. When the table
has two records, the LINQ expression correctly finds the matching row (row 2). Could the default somehow be the first row?
Update: Even the condition with multiple rows did it, but it does not do it consistently. On trying to repeat the error, it didn't happen.