Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. how to check for null inside LINQ?

how to check for null inside LINQ?

Scheduled Pinned Locked Moved LINQ
databasecsharplinqhelptutorial
3 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    balaji t
    wrote on last edited by
    #1

    can any1 tell me how 2 check is null for a column in LINQ? for e.g var x = (from c in db.stock select new {c.stockid}).distinct(); then i gave int c = x.max(a=>a.stockid)+1; it works but i want to chek whether stockid is null.if its null then i will replace it with 0 my original query like this select distinct max(isnull(stockid,0))+1 from stock i want this in LINQ ..kindly help me

    T.Balaji

    L M 2 Replies Last reply
    0
    • B balaji t

      can any1 tell me how 2 check is null for a column in LINQ? for e.g var x = (from c in db.stock select new {c.stockid}).distinct(); then i gave int c = x.max(a=>a.stockid)+1; it works but i want to chek whether stockid is null.if its null then i will replace it with 0 my original query like this select distinct max(isnull(stockid,0))+1 from stock i want this in LINQ ..kindly help me

      T.Balaji

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      where x != null

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 alpha 4a out now (29 May 2008)

      1 Reply Last reply
      0
      • B balaji t

        can any1 tell me how 2 check is null for a column in LINQ? for e.g var x = (from c in db.stock select new {c.stockid}).distinct(); then i gave int c = x.max(a=>a.stockid)+1; it works but i want to chek whether stockid is null.if its null then i will replace it with 0 my original query like this select distinct max(isnull(stockid,0))+1 from stock i want this in LINQ ..kindly help me

        T.Balaji

        M Offline
        M Offline
        Mirko1980
        wrote on last edited by
        #3

        The C# equivalent of isnull is the ?? operator. So, you can do the following:

        var x = (from a in db.stock
        select new { a.stockid }).Distinct();
        int c = x.Max(a => a.stockid ?? 0) + 1;

        Or, if you want to filter null values in the query:

        var x = (from a in db.stock
        select new { stockid = (a.stockid ?? 0) }).Distinct();
        int c = x.Max(a => a.stockid) + 1;

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups