Solved

Pivot Sql Table column to a list c#

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Nested forach loop to linq 3 45
Make borderless form movable by user 2 27
Need a quick overview of Selenium testing 2 36
Manage big list of parameter list 8 26
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

752 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