Solved

Pivot Sql Table column to a list c#

Posted on 2010-08-13
5
1,910 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 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now