Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pivot Sql Table column to a list c#

Posted on 2010-08-13
5
Medium Priority
?
2,030 Views
Last Modified: 2013-12-17

Hi,

I have a question about how to pivot a sql table column into a list of objects.

Assume I have a product table with columns:

product id    location    price
    1                 x             $10
    1                 y             $12
    1                 z             $14

I created a (location, price) class and a product class as:

public class priceLocation
{
      public string location;
      public decimal price;
}

and
public class product
{
      public int productid;
      public list<priceLocation> priceLocations;
}
public list<object> locationprice

How can I load the above sql data to these classes?

Thanks
0
Comment
Question by:sofux
  • 3
5 Comments
 
LVL 5

Expert Comment

by:JayFromPep
ID: 33432685
I think that LINQ would be the way to go.

Check this article....

http://www.c-sharpcorner.com/UploadFile/scottlysle/L2SinCS06022008035847AM/L2SinCS.aspx
0
 

Author Comment

by:sofux
ID: 33433790
I am not sure how it helps.

Let me clarify little bit:

product id    location    price
    1                 x             $10
    1                 y             $12
    1                 z             $14
    2                 x             $15
    2                 y             $17
    2                 z             $13

need to convert this table to create a product list. If I had to do it manually, it would look like this:

list<product> Products = new list<product>
    { new Product{productid = 1, new List<priceLocation>{new priceLocation{location = x, price =10},
                                                                                            new priceLocation{location = x, price =12},
                                                                                            new priceLocation{location = x, price =14}}},
       new Product{productid = 2, new List<priceLocation>{new priceLocation{location = x, price =15},
                                                                                            new priceLocation{location = x, price =17},
                                                                                            new priceLocation{location = x, price =13}}}};

I am familiar with linq but not sure if this can be accomplished with linq.
Thanks.


0
 
LVL 2

Accepted Solution

by:
Algol36 earned 1500 total points
ID: 33436629
       public class priceLocation
        {
              public string location;
              public decimal price;
        }

        public class product
        {
              public int productid;
              public List<priceLocation> priceLocations;
        }
        public List<product> locationprice;

        public void Run()
        {
            //create example datatable
            DataTable table = new DataTable();
            table.Columns.Add("productId", typeof(int));
            table.Columns.Add("location", typeof(string));
            table.Columns.Add("price", typeof(decimal));
            table.Rows.Add(1, "x", "10");
            table.Rows.Add(1, "y", "20");
            table.Rows.Add(1, "z", "15");
            table.Rows.Add(2, "x", "10");
            //using linq to extract data into classes
            var query = from prod in table.AsEnumerable()
                        group prod by prod["productId"]
                            into grp
                            select new product
                            {
                                productid = (int)grp.Key,
                                priceLocations = grp.Select<DataRow, priceLocation>(
                                    l => new priceLocation() {
                                        location = (string)l["location"],
                                        price = (decimal)l["price"]
                                        }
                                    ).ToList()
                            };

            locationprice = query.Cast<product>().ToList<product>();
        }
0
 

Author Comment

by:sofux
ID: 33441872
Thanks.

Need to small adjustments but overall it works.

This part of the code didn't work:

grp.Select<DataRow, priceLocation>(
                                    l => new priceLocation() {
                                        location = (string)l["location"],
                                        price = (decimal)l["price"]
                                        }

so I replaced this with:

grp.Select( l => new priceLocation() {
                                        location = l.location,
                                        price = l.price
                                        }

0
 

Author Closing Comment

by:sofux
ID: 33441878
Provided very good direction but further improvements required on my side.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Loops Section Overview
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question