简体   繁体   中英

Transforming records structured in a one-to-many relationship into a list of object in C#

I have table structured like this:

PrimaryKey |ColumnDesc   |ColumnValue
------------------------------------
Object1    |Name - First |Steve
Object1    |Height - in  |60
Object1    |Weight - lbs |180.3
Object2    |Name - First |Edward
Object2    |Height - in  |62
Object2    |Weight - lbs |200.0

When I pull the data from the database, I want to turn it from the structure above into a list of objects, with the following class structure:

public class Person
{
     public string Name { get; set; }
     public int Height { get; set; }
     public double Weight { get; set; }
}

This way it can be saved to another table (called Person), with a structure like this:

PersonId |Name   |Height  |Weight
---------------------------------
10000    |Steve  |60      |180.3
10001    |Edward |62      |200.0

I'm running into difficulty figuring out the best possible way to do this. I'm envisioning a mapping table set up like this:

OriginalColumName |NewColumnName|DataType
-----------------------------------------
Name - First      |Name         |string
Height - in       |Height       |int
Weight - lbs      |Weight       |double

A method would group the records from the first table structure based on the "PrimaryKey" field and then use the mapping table and reflection to assign the values and type to each object. I'm struggling to come up with the proper syntax to accomplish this (especially concerning the varying types), and I'm wondering if there is a more succinct and logical way of fulfilling this requirement in code. Does a better solution already exists or does anyone have advice on the best route to accomplishing this?

IF the first table is called table1 then this would work in SQL -- you can use this in an insert statement to make the person table.

 SELECT BASE.primarykey, first.columnvalue as name, height.columnvalue as height, weight.columnvalue as weight
 FROM (
   SELECT DISNTCT PrimaryKey 
   FROM table1
 ) as BASE
 LEFT JOIN table1 as first on base.primarykey = first.primarykey and first.columndesc = 'Name - First' 
 LEFT JOIN table1 as height on base.primarykey = height.primarykey and height.columndesc = 'Height - in' 
 LEFT JOIN table1 as weight on base.primarykey = weight.primarykey and weight.columndesc = 'Weight - lbs' 

Depending on your database and the method you use to connect to it, I believe this would be a good place for a PIVOT table:

Assuming table PivotTest looks like:

CREATE TABLE PivotTest (
    PrimaryKey int not null,
    ColumnDesc varchar(30),
    ColumnValue varchar(100)
)

INSERT INTO PivotTest (PrimaryKey, ColumnDesc, ColumnValue)
  VALUES
  -- PrimaryKey,   ColumnDesc, ColumnValue 
    (     10000, 'Name-First',      'Steve'),
    (     10000,  'Height-In',         '60'),
    (     10000, 'Weight-Lbs',      '180.3'),
    (     10001, 'Name-First',     'Edward'),
    (     10001,  'Height-In',         '62'),
    (     10001, 'Weight-Lbs',      '200.0')

Using a PIVOT Table in SQL SERVER the syntax would look like:

SELECT * 
  FROM
    (SELECT 
        Id,
        Data,
        Description
    FROM PivotTest) AS Pvt
PIVOT (
    MIN(Data)
    FOR Description IN ([Name - First], [Height - in], [Weight - lbs])
) AS Result

Here are some helpful links to further explain PIVOT and UNPIVOT:

Microsoft ,

Oracle ,

CodingSight

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM