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. Web Development
  3. ASP.NET
  4. How to Handle Null using LINQ during Datatable CrossJoin C#

How to Handle Null using LINQ during Datatable CrossJoin C#

Scheduled Pinned Locked Moved ASP.NET
csharplinqhelptutorial
3 Posts 2 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.
  • P Offline
    P Offline
    priyaahh
    wrote on last edited by
    #1

    Hi Friends I have multiple datatables, and the datatables may or may not have data, depends on the user selection. I have to get Cartesian Product of all the Datatables, and pass the Cartesian Product result as datasource for Gridview. I am using following code which is working fine if all the DataTable has rows. If any of the table is not having row, then I am getting no records in my resultant cartesian product table. Please help in this. var newDatatable = from TowerName in dtTower.AsEnumerable() from CountryName in dtCountry.AsEnumerable() from Level in dtLevel.AsEnumerable() from CityName in dtCity.AsEnumerable() from BandName in dtBand.AsEnumerable() from EType in dtEmploymentType.AsEnumerable() from Skill in dtSkill.AsEnumerable() select new { TowerName, CountryName, Level, CityName, BandName, EType, Skill }; DataTable dtlinq = new DataTable("poclinq"); foreach (var item in newDatatable) { DataRow dr = dtlinq.NewRow(); dr["TowerName"] = item.TowerName.ItemArray[0]; dr["CountryName"] = item.CountryName.ItemArray[0]; dr["Level"] = item.Level.ItemArray[0]; dr["CityName"] = item.CityName.ItemArray[0]; dr["BandName"] = item.BandName.ItemArray[0]; dr["EType"] = item.EType.ItemArray[0]; dr["Skill"] = item.Skill.ItemArray[0]; dtlinq.Rows.Add(dr); }

    Richard DeemingR 1 Reply Last reply
    0
    • P priyaahh

      Hi Friends I have multiple datatables, and the datatables may or may not have data, depends on the user selection. I have to get Cartesian Product of all the Datatables, and pass the Cartesian Product result as datasource for Gridview. I am using following code which is working fine if all the DataTable has rows. If any of the table is not having row, then I am getting no records in my resultant cartesian product table. Please help in this. var newDatatable = from TowerName in dtTower.AsEnumerable() from CountryName in dtCountry.AsEnumerable() from Level in dtLevel.AsEnumerable() from CityName in dtCity.AsEnumerable() from BandName in dtBand.AsEnumerable() from EType in dtEmploymentType.AsEnumerable() from Skill in dtSkill.AsEnumerable() select new { TowerName, CountryName, Level, CityName, BandName, EType, Skill }; DataTable dtlinq = new DataTable("poclinq"); foreach (var item in newDatatable) { DataRow dr = dtlinq.NewRow(); dr["TowerName"] = item.TowerName.ItemArray[0]; dr["CountryName"] = item.CountryName.ItemArray[0]; dr["Level"] = item.Level.ItemArray[0]; dr["CityName"] = item.CityName.ItemArray[0]; dr["BandName"] = item.BandName.ItemArray[0]; dr["EType"] = item.EType.ItemArray[0]; dr["Skill"] = item.Skill.ItemArray[0]; dtlinq.Rows.Add(dr); }

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      priyaahh wrote:

      If any of the table is not having row, then I am getting no records in my resultant cartesian product table.

      The Cartesian product of any set with the empty set is the empty set. That's fairly basic set theory. You'll need a custom extension method which will return a single empty row if the table is empty:

      public static class DataTableExtensions
      {
      public static IEnumerable<DataRow> AsEnumerableNotEmpty(this DataTable table)
      {
      if (table == null) throw new ArgumentNullException("table");
      if (table.Rows.Count != 0) return table.Rows.Cast<DataRow>();
      return new[] { table.NewRow() };
      }
      }

      You'll then need to change your LINQ query to call the custom method:

      var newDatatable = from TowerName in dtTower.AsEnumerableNotEmpty()
      from CountryName in dtCountry.AsEnumerableNotEmpty()
      from Level in dtLevel.AsEnumerableNotEmpty()
      from CityName in dtCity.AsEnumerableNotEmpty()
      from BandName in dtBand.AsEnumerableNotEmpty()
      from EType in dtEmploymentType.AsEnumerableNotEmpty()
      from Skill in dtSkill.AsEnumerableNotEmpty()
      select new { TowerName, CountryName, Level, CityName, BandName, EType, Skill };


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      P 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        priyaahh wrote:

        If any of the table is not having row, then I am getting no records in my resultant cartesian product table.

        The Cartesian product of any set with the empty set is the empty set. That's fairly basic set theory. You'll need a custom extension method which will return a single empty row if the table is empty:

        public static class DataTableExtensions
        {
        public static IEnumerable<DataRow> AsEnumerableNotEmpty(this DataTable table)
        {
        if (table == null) throw new ArgumentNullException("table");
        if (table.Rows.Count != 0) return table.Rows.Cast<DataRow>();
        return new[] { table.NewRow() };
        }
        }

        You'll then need to change your LINQ query to call the custom method:

        var newDatatable = from TowerName in dtTower.AsEnumerableNotEmpty()
        from CountryName in dtCountry.AsEnumerableNotEmpty()
        from Level in dtLevel.AsEnumerableNotEmpty()
        from CityName in dtCity.AsEnumerableNotEmpty()
        from BandName in dtBand.AsEnumerableNotEmpty()
        from EType in dtEmploymentType.AsEnumerableNotEmpty()
        from Skill in dtSkill.AsEnumerableNotEmpty()
        select new { TowerName, CountryName, Level, CityName, BandName, EType, Skill };


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        P Offline
        P Offline
        priyaahh
        wrote on last edited by
        #3

        Hi Richard, Thanks lot for your response, it working for me. Regards, Priyaa.

        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