Solved

Pivot Sql Table column to a list c#

Posted on 2010-08-13
5
1,953 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSRS Deployment problem 5 93
Retrieve Active Directory Groups a User belongs to in VB.NET 3 44
Amazon S3 .Net error 5 40
IDE for Python 5 67
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the adminiā€¦

756 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