Last post Jun 15, 2015 04:06 PM by infusednz
Jun 15, 2015 04:38 AM|infusednz|LINK
I'm trying to do something like this:
var q = (from i in ctx.Images
where i.Hash is null
and return multi lines, but as you know, nulls don't work. The hash is type varchar and allows nulls.
Jun 15, 2015 05:05 AM|infusednz|LINK
ah was doing it wrong. figured it out.
Jun 15, 2015 05:09 AM|PatriceSc|LINK
Likely you changed to i.Hash==null. The point is that this is really C# rather than SQL.
When solving your own issue, it is still best to tell what you have done (so that someone finding this thread and having the same problem can get the solution).
Jun 15, 2015 07:16 AM|infusednz|LINK
Sure. You were correct. I was trying to use DbNull or something as I didn't think you could write statements with actual c# code in them?
Jun 15, 2015 07:20 AM|Mikesdotnetting|LINK
I didn't think you could write statements with actual c# code in them?
LINQ is pure C# (or VB). It is translated by the Entity Framework into SQL to be executed against the database.
Jun 15, 2015 07:42 AM|PatriceSc|LINK
DbNull is for "classic ADO.NET" with DataTables etc...
The whole point of LINQ is to bring into C# a "domain specific language" so the query you wrote is ultimately just the same C# method based code you could have written yourself that is:
var q = ctx.Images.Where(o => o.Hash == null);
So even your SQL looking statement is just actually compiled to C# code under the hood. Linq to EF then translates this into a SQL statement (and you'll have an exception if the expression can't be translated to a SQL statement).
Jun 15, 2015 08:09 AM|infusednz|LINK
Thanks for both replies.
What's the difference between what you've written there, vs what I wrote? Is that the fluent api? I'm still trying to get my head around that, and which one works best going forward.
Jun 15, 2015 09:32 AM|Mikesdotnetting|LINK
Is that the fluent api?
Yes. Most professional developers use that approach. The syntax you used is a lot more like SQL, but it's down to personal preference really. They both produce the same results.
Jun 15, 2015 09:59 AM|PatriceSc|LINK
There is really no difference. The first form is translated at compile time into the second one. This is just to show that you always end up with C# code.
My personal preference is to use the 2nd form for simple thing as it is more compact and still legible. For complex queries, the first form is likely more legible but it is just a personal preference....
Jun 15, 2015 04:06 PM|infusednz|LINK
Thanks again for both replies. Cleared quite a bit up.