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. NULL problem in string concatination in LINQ

NULL problem in string concatination in LINQ

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqhelptutorial
6 Posts 3 Posters 0 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.
  • K Offline
    K Offline
    K V Sekhar
    wrote on last edited by
    #1

    Hi all, I have a table 'tbl_titles'. The content of table is like below.

    id title subtitle cost

    1 NULL NULL NULL
    2 NULL 0
    3 test subtest 100
    4
    5 test1 subtest1 500
    6 NULL 0
    7 test2 400
    8 subtest2 300
    9 test3 NULL 200
    10 NULL subtest4 100

    i have to get output by concatinating title & subtitle. Like below

    id name cost

    3 test subtest 100
    5 test1 subtest1 500
    7 test2 400
    8 subtest2 300
    9 test3 200
    10 subtest4 100

    for that i wrote query like below. var query = from t in dataContext.tbl_titles where t.title.Length >0 || t.subtitle.Length > 0 select new { name = t.title + t.subtitle }; but i am getting NULL values as name even though any one of the string is not null or empty. Please suggest me, how to write query inorder to get output like above. Thanks in advance.

    A K 2 Replies Last reply
    0
    • K K V Sekhar

      Hi all, I have a table 'tbl_titles'. The content of table is like below.

      id title subtitle cost

      1 NULL NULL NULL
      2 NULL 0
      3 test subtest 100
      4
      5 test1 subtest1 500
      6 NULL 0
      7 test2 400
      8 subtest2 300
      9 test3 NULL 200
      10 NULL subtest4 100

      i have to get output by concatinating title & subtitle. Like below

      id name cost

      3 test subtest 100
      5 test1 subtest1 500
      7 test2 400
      8 subtest2 300
      9 test3 200
      10 subtest4 100

      for that i wrote query like below. var query = from t in dataContext.tbl_titles where t.title.Length >0 || t.subtitle.Length > 0 select new { name = t.title + t.subtitle }; but i am getting NULL values as name even though any one of the string is not null or empty. Please suggest me, how to write query inorder to get output like above. Thanks in advance.

      A Offline
      A Offline
      Abhishek Sur
      wrote on last edited by
      #2

      Check title/subtitle is Null or not before calling its Length :cool:

      Abhishek Sur My Latest Articles Working with Excel using MDAC
      Basics on LINQ and Lambda Expressions
      Create .NET Templates

      K 1 Reply Last reply
      0
      • K K V Sekhar

        Hi all, I have a table 'tbl_titles'. The content of table is like below.

        id title subtitle cost

        1 NULL NULL NULL
        2 NULL 0
        3 test subtest 100
        4
        5 test1 subtest1 500
        6 NULL 0
        7 test2 400
        8 subtest2 300
        9 test3 NULL 200
        10 NULL subtest4 100

        i have to get output by concatinating title & subtitle. Like below

        id name cost

        3 test subtest 100
        5 test1 subtest1 500
        7 test2 400
        8 subtest2 300
        9 test3 200
        10 subtest4 100

        for that i wrote query like below. var query = from t in dataContext.tbl_titles where t.title.Length >0 || t.subtitle.Length > 0 select new { name = t.title + t.subtitle }; but i am getting NULL values as name even though any one of the string is not null or empty. Please suggest me, how to write query inorder to get output like above. Thanks in advance.

        K Offline
        K Offline
        Kunal Chowdhury IN
        wrote on last edited by
        #3

        use the following code snippets:

        var query = from t in dataContext.tbl_titles
        where
        (
        (!string.isNullOrEmpty(t.title) && t.title.Length > 0)
        ||
        (!string.isNullOrEmpty(t.subtitle) && t.subtitle.Length > 0)
        )
        select new { name = t.title + t.subtitle };

        Regards, - Kunal Chowdhury (My Blog)

        K 1 Reply Last reply
        0
        • K Kunal Chowdhury IN

          use the following code snippets:

          var query = from t in dataContext.tbl_titles
          where
          (
          (!string.isNullOrEmpty(t.title) && t.title.Length > 0)
          ||
          (!string.isNullOrEmpty(t.subtitle) && t.subtitle.Length > 0)
          )
          select new { name = t.title + t.subtitle };

          Regards, - Kunal Chowdhury (My Blog)

          K Offline
          K Offline
          K V Sekhar
          wrote on last edited by
          #4

          Thanks for your reply.

          K 1 Reply Last reply
          0
          • A Abhishek Sur

            Check title/subtitle is Null or not before calling its Length :cool:

            Abhishek Sur My Latest Articles Working with Excel using MDAC
            Basics on LINQ and Lambda Expressions
            Create .NET Templates

            K Offline
            K Offline
            K V Sekhar
            wrote on last edited by
            #5

            Thanks for your reply. I will go through it.

            1 Reply Last reply
            0
            • K K V Sekhar

              Thanks for your reply.

              K Offline
              K Offline
              K V Sekhar
              wrote on last edited by
              #6

              here is sample

              var query = from t in dataContext.Users
              let fname = t.fname != null ? t.fname : " "
              let lname = t.lname != null ? t.lname : " "
              select new {name = fname.Tostring()+ " "+lname.ToString()};

              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