Solved

Pivot Sql Table column to a list c#

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
which object should i use (efficiency) 5 45
coded character set iptc field within a jpeg 1 67
Help with sorting data in Listbox using VB.NET 3 53
Exit the loop 4 22
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

948 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

22 Experts available now in Live!

Get 1:1 Help Now