1

My problem at the moment is I have results coming back from a SQL query that returns a result like this:

125 Month   10.00   Wholesale
125 Year    20.00   Wholesale
126 Month   20.00   Wholesale
126 Year    30.00   Wholesale
127 Month   40.00   Wholesale
127 Year    50.00   Wholesale

where integer column is the ID of the column. when the data gets returned to the C# calling code, it is placed into an object followering this structure:

PuctName; }

I am just having issues with how to create the terms without causing an endless amount of loops.

Ceri Westcott
  • 300
  • 2
  • 14

3 Answers3

2

you can use a Dictionary that way you will have a key value pair. the key is ProductID and the value the list of Terms.

var dictionary = new Dictionary<int, List<Terms>>();
foreach (ProductTermAndPricingDataItem item in productInformationItems)
{
            if(dictionary.ContainsKey(item.ProductID))
            {
                dictionary[item.ProductID].Add(new Terms { Term = item.BillingPeriodName, Price = item.PriceAmount});
            }
            else
            {
                dictionary.Add(item.ProductID, new List<Terms>() { new Terms() {Term = item.BillingPeriodName, Price = item.PriceAmount             } });
            }  
 }
Overmachine
  • 1,723
  • 3
  • 15
  • 27
1

You can use Linq and GroupBy:

List<ProductPricingGetDataItem> grouped = productInformationItems.GroupBy(
                                        p => p.ProductID,
                                        (key, g) => new ProductPricingGetDataItem() { ProductID = key, Terms = g.Select(x => new Terms(x.BillingPeriodName, x.PriceAmount)).ToList() }).ToList();

In order for that code to work, you need to add a constructor to Terms :

public Terms(string term, decimal price)
{
    Term = term;
    Price = price;
}

Fiddle with working example : https://dotnetfiddle.net/EE2BpP

Fourat
  • 2,366
  • 4
  • 38
  • 53
1

For LINQ lovers:

//Your initial data list
var productInformationItems = new List<ProductTermAndPricingDataItem>();
var productPricingGetDataItems = productInformationItems.ToLookup(item => item.ProductID)
                                                        .Select(grouping => new ProductPricingGetDataItem
                                                        {
                                                            ProductID = grouping.Key,
                                                            Terms = grouping.Select(item => new Terms
                                                            {
                                                                Price = item.PriceAmount,
                                                                Term = item.BillingPeriodName
                                                            }).ToList()
                                                        }).ToList();

In your exact case the result is :

enter image description here

Feel free to ask if something is not clear.

Vasilievski
  • 773
  • 6
  • 13