I have a user list, some users do not have a name (empty). If I run the first LINQ query, then I found an error saying "Object Reference is not set for an example of an object" error.
var temp = (one of the user lists (where (a name == "John") & amp; amp; (a.name! = Null)) Select one) Olist ();
However, if I switch the order by checking the tap on the front, then it works without throwing an error:
var temp = (From a user list where ((a.name! = Null) & amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; a.name ==>
Why is that so? If this is a pure C # code (not a LINQ), then I think that the two will be the same. I do not have a SQL Profiler, I'm just curious what would be the difference if they are being translated at the SQL level.
In C # there is short-circuiting, so if the first condition falsifies, then the second condition is absolutely Does not execute. From MSDN:
Conditional-end operator (& amp; amp;) operates a logical-and its bolus, but only evaluates its second operand if necessary.
behaves in a similar way, except that this second argument is not correct if this first return is correct.
I do not think this is the whole story, though. My remaining post contains the following points:
- You can log SQL statements using DataContext.Log.
- Your query should not produce an error in any way you write it.
- The difference between the object from LINQ and the behavior of SQL from LINQ.
- Your filtering can be performed locally at the database instead.
You can easily view the generated SQL in Visual Studio without the need of a SQL Profiler. You can hover your mouse over a SQL query object from a LINQ and it will display the SQL. Or you can use the SQL statement to log in, for example:
TextWriter textWriter = new stringwriter (); (Var dc = New UserDataContext ()) using {dc.log = textWriter; Var userList = dc.Users; Var temp = (where in a user list (a.Name.ToString () == "John") & amp; amp; amp; amp; amp; amp; amp; amp; amp ; Amp; one.} String log = textWriter.ToString ();
You can also enter a file or even Console.Out < / Code>:
dc.log = Console.Out;
By doing this you can see that the query looks something like this, even though you have There will be more columns in the selection list:
SELECT [t0] [name] to [dbo]. [User] AS [T] WHERE ([T.]. [Name] = @P 0) and ([T.]. [Name] No
Another issue is that your query should not produce an error even if a.name
is zero, A == "john"
should still work - it will change now.
Finally, how C # works normally and how SQL works by LINQ The difference between you should not get a blank exception from the database. To display it I will do a small amendment in your query. - ToString
after a.Name
:
var temp = in a user list where (a.Name.ToString () == "Jones") & amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; Amp; A & #
Now it fails for an object with an NullReferenceException, but it works with LINQ to SQL without throwing an exception. So I suspect that you have loaded all the items from the database into memory and filtered locally. In other words, you have something like this:
var userList = dc.Users.ToList ();
Instead of the following which will allow the database to be filtered:
var userList = dc.Users;
So I suspect that meeting with the eye is more for this question. Perhaps you can provide more information.
Comments
Post a Comment