locked
EF 6.1 select where null RRS feed

  • Question

  • User-1766430311 posted

    I'm trying to do something like this:

    var q = (from i in ctx.Images
                            where i.Hash is null
                            select i);

    and return multi lines, but as you know, nulls don't work. The hash is type varchar and allows nulls.

    Monday, June 15, 2015 4:38 AM

Answers

  • User753101303 posted

    Hi,

    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).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2015 5:09 AM
  • User-821857111 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2015 9:32 AM
  • User753101303 posted

    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....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2015 9:59 AM

All replies

  • User-1766430311 posted

    ah was doing it wrong. figured it out.

    Monday, June 15, 2015 5:05 AM
  • User753101303 posted

    Hi,

    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).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2015 5:09 AM
  • User-1766430311 posted

    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?

    Monday, June 15, 2015 7:16 AM
  • User-821857111 posted

    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.

    Monday, June 15, 2015 7:20 AM
  • User753101303 posted

    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).

    Monday, June 15, 2015 7:42 AM
  • User-1766430311 posted

    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.

    Monday, June 15, 2015 8:09 AM
  • User-821857111 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2015 9:32 AM
  • User753101303 posted

    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....

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 15, 2015 9:59 AM
  • User-1766430311 posted

    Thanks again for both replies. Cleared quite a bit up.

    Monday, June 15, 2015 4:06 PM