Dynamic property assignment based on data source? RRS feed

  • Question

  • Say I have a class:


    public Subcontractor {

      public string Id { get; set; }

      public string SName { get; set; }

      public string Name { get; set; }

      public string Contact { get; set; }



    I am pulling data from FoxPro DBFs; here are two sample queries:



      `subname` AS `SName`,

      `full_name` AS `Name`

    FROM `wssubc`




      `id` AS `Id`,

      `contact` AS `Contact`

    FROM `wssubc`


    Now say I'm running those queries with a code sequence as follows:


    // Create and execute a command

    OleDbCommand cmd = conn.CreateCommand();

    cmd.CommandText = @" /* query here */ ";

    OleDbDataReader data = cmd.ExecuteReader();

    if(data.HasRows) {

      while(data.Read()) {

        // Do something




    What I'd like to do is create a List<Subcontractor>, setting only those properties present in my query.


    Additionally, I'd like to be able to do this as generically as possible with a function that takes the query to run and the type of object to create the list out of.  The reason is because ultimately I will be pulling from multiple data sources and filling many different objects.  While I could hand-code all this out, I'd rather not.


    Thanks for any assistance!

    Friday, March 7, 2008 11:21 PM


All replies