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. C#
  4. ColumnMapping to properties of an class object

ColumnMapping to properties of an class object

Scheduled Pinned Locked Moved C#
databasequestionworkspace
2 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.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I have a class like so:

    public class BaseRef
    {
    public int id {get; set;}
    public string listID {get; set;}
    public string Name {get; set;}
    }

    And have a data column that is of type BaseRef that I added to a DataTable:

    this.Columns.Add(new DataColumn("ParentRef", typeof(BaseRef)));

    I am using OdbcDataAdapter to get a query, but in this case my query has three separate columns for the id, listID and Name fields of the Parent:

    SELECT id, listID, Name FROM ParentTable

    I tried to setup a DataAdapterTableMapping and add ColumnMappings, but it doesn't appear that I can map a source column to a property of a class column. I tried:

    mapping.ColumnMappings.Add("id", "ParentRef.id");
    mapping.ColumnMappings.Add("listID", "ParentRef.listID");
    mapping.ColumnMappings.Add("Name", "ParentRef.Name");

    Is there a way to get the mapping to map to the properties of my class?

    Richard DeemingR 1 Reply Last reply
    0
    • H hpjchobbes

      I have a class like so:

      public class BaseRef
      {
      public int id {get; set;}
      public string listID {get; set;}
      public string Name {get; set;}
      }

      And have a data column that is of type BaseRef that I added to a DataTable:

      this.Columns.Add(new DataColumn("ParentRef", typeof(BaseRef)));

      I am using OdbcDataAdapter to get a query, but in this case my query has three separate columns for the id, listID and Name fields of the Parent:

      SELECT id, listID, Name FROM ParentTable

      I tried to setup a DataAdapterTableMapping and add ColumnMappings, but it doesn't appear that I can map a source column to a property of a class column. I tried:

      mapping.ColumnMappings.Add("id", "ParentRef.id");
      mapping.ColumnMappings.Add("listID", "ParentRef.listID");
      mapping.ColumnMappings.Add("Name", "ParentRef.Name");

      Is there a way to get the mapping to map to the properties of my class?

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

      I don't think that's possible. The DataAdapter wasn't designed to load columns to/from nested classes. You'd probably have better luck using Dapper[^] instead.


      "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

      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