Multiple grouping [duplicate]









up vote
0
down vote

favorite













This question already has an answer here:



  • Group By Multiple Columns

    12 answers



I have a task - "Group all products into categories, inside - by availability in stock, inside the last group, group by price."



Data:



var productList =
new List<Product>
new Product ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 ,
new Product ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 ,
new Product ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 ,
new Product ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 ,
new Product ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 ,
new Product ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 ,
new Product ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 ,
new Product ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 ,
new Product ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 ,
new Product ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 ,
new Product ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 ,
new Product ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 ,
new Product ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 ,
new Product ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 ,
new Product ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 ,
new Product ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 ,
new Product ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 ,
new Product ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57
;


Here is the solution that I came up with, but I can not compare stock and price.



var category = productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
UnitsInStock = s.Select(s2 => s2.UnitsInStock),
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitsInStock = category.GroupBy(g=> new g.Category, g.UnitsInStock)
.Select(s => new

UnitsInStock = s.Key.UnitsInStock,
Category = s.Key.Category,
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitPrice = unitsInStock.GroupBy(g => new g.Category, g.UnitsInStock, g.UnitPrice)
.Select(s => new

UnitPrice = s.Key.UnitPrice,
Category = s.Key.Category,
UnitsInStock = s.Key.UnitsInStock
);

foreach (var categ in unitPrice)

Console.WriteLine($"Category: categ.Category");

foreach (var stock in categ.UnitsInStock)

Console.WriteLine($"UnitsInStock: stock");

foreach (var price in categ.UnitPrice.SelectMany(s => s))

Console.WriteLine($"UnitPrice: price");




enter image description here
I want stock and price to be in one line



Expected result:



enter image description here



The entire complexity of the task is that it is necessary from the beginning from grouping by Category, then by UnitsInStock, then UnitPrice. And all this output is grouped










share|improve this question















marked as duplicate by TaW c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
19 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • what is the desired result? could you provide an example class of the result?
    – Ashkan Mobayen Khiabani
    19 hours ago










  • Not sure the retirement makes sense. In the last group, if you have two products with the same units in stock to group on, then what price do you show?
    – Paddy
    19 hours ago










  • @AshkanMobayenKhiabani, yes ofc
    – Tibomso
    19 hours ago










  • @Tibomso well then Enigmativity's answer is exactly what you want
    – Ashkan Mobayen Khiabani
    19 hours ago











  • @AshkanMobayenKhiabani, yes,but if I understood correctly from his answer, I need to remove my groupings and make only one, although on the task I need to be grouped from the beginning: Category -> UnitsInStock->UnitPrice
    – Tibomso
    19 hours ago














up vote
0
down vote

favorite













This question already has an answer here:



  • Group By Multiple Columns

    12 answers



I have a task - "Group all products into categories, inside - by availability in stock, inside the last group, group by price."



Data:



var productList =
new List<Product>
new Product ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 ,
new Product ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 ,
new Product ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 ,
new Product ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 ,
new Product ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 ,
new Product ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 ,
new Product ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 ,
new Product ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 ,
new Product ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 ,
new Product ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 ,
new Product ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 ,
new Product ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 ,
new Product ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 ,
new Product ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 ,
new Product ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 ,
new Product ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 ,
new Product ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 ,
new Product ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57
;


Here is the solution that I came up with, but I can not compare stock and price.



var category = productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
UnitsInStock = s.Select(s2 => s2.UnitsInStock),
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitsInStock = category.GroupBy(g=> new g.Category, g.UnitsInStock)
.Select(s => new

UnitsInStock = s.Key.UnitsInStock,
Category = s.Key.Category,
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitPrice = unitsInStock.GroupBy(g => new g.Category, g.UnitsInStock, g.UnitPrice)
.Select(s => new

UnitPrice = s.Key.UnitPrice,
Category = s.Key.Category,
UnitsInStock = s.Key.UnitsInStock
);

foreach (var categ in unitPrice)

Console.WriteLine($"Category: categ.Category");

foreach (var stock in categ.UnitsInStock)

Console.WriteLine($"UnitsInStock: stock");

foreach (var price in categ.UnitPrice.SelectMany(s => s))

Console.WriteLine($"UnitPrice: price");




enter image description here
I want stock and price to be in one line



Expected result:



enter image description here



The entire complexity of the task is that it is necessary from the beginning from grouping by Category, then by UnitsInStock, then UnitPrice. And all this output is grouped










share|improve this question















marked as duplicate by TaW c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
19 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • what is the desired result? could you provide an example class of the result?
    – Ashkan Mobayen Khiabani
    19 hours ago










  • Not sure the retirement makes sense. In the last group, if you have two products with the same units in stock to group on, then what price do you show?
    – Paddy
    19 hours ago










  • @AshkanMobayenKhiabani, yes ofc
    – Tibomso
    19 hours ago










  • @Tibomso well then Enigmativity's answer is exactly what you want
    – Ashkan Mobayen Khiabani
    19 hours ago











  • @AshkanMobayenKhiabani, yes,but if I understood correctly from his answer, I need to remove my groupings and make only one, although on the task I need to be grouped from the beginning: Category -> UnitsInStock->UnitPrice
    – Tibomso
    19 hours ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:



  • Group By Multiple Columns

    12 answers



I have a task - "Group all products into categories, inside - by availability in stock, inside the last group, group by price."



Data:



var productList =
new List<Product>
new Product ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 ,
new Product ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 ,
new Product ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 ,
new Product ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 ,
new Product ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 ,
new Product ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 ,
new Product ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 ,
new Product ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 ,
new Product ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 ,
new Product ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 ,
new Product ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 ,
new Product ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 ,
new Product ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 ,
new Product ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 ,
new Product ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 ,
new Product ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 ,
new Product ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 ,
new Product ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57
;


Here is the solution that I came up with, but I can not compare stock and price.



var category = productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
UnitsInStock = s.Select(s2 => s2.UnitsInStock),
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitsInStock = category.GroupBy(g=> new g.Category, g.UnitsInStock)
.Select(s => new

UnitsInStock = s.Key.UnitsInStock,
Category = s.Key.Category,
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitPrice = unitsInStock.GroupBy(g => new g.Category, g.UnitsInStock, g.UnitPrice)
.Select(s => new

UnitPrice = s.Key.UnitPrice,
Category = s.Key.Category,
UnitsInStock = s.Key.UnitsInStock
);

foreach (var categ in unitPrice)

Console.WriteLine($"Category: categ.Category");

foreach (var stock in categ.UnitsInStock)

Console.WriteLine($"UnitsInStock: stock");

foreach (var price in categ.UnitPrice.SelectMany(s => s))

Console.WriteLine($"UnitPrice: price");




enter image description here
I want stock and price to be in one line



Expected result:



enter image description here



The entire complexity of the task is that it is necessary from the beginning from grouping by Category, then by UnitsInStock, then UnitPrice. And all this output is grouped










share|improve this question
















This question already has an answer here:



  • Group By Multiple Columns

    12 answers



I have a task - "Group all products into categories, inside - by availability in stock, inside the last group, group by price."



Data:



var productList =
new List<Product>
new Product ProductID = 1, ProductName = "Chai", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 39 ,
new Product ProductID = 2, ProductName = "Chang", Category = "Beverages", UnitPrice = 19.0000M, UnitsInStock = 17 ,
new Product ProductID = 9, ProductName = "Mishi Kobe Niku", Category = "Meat/Poultry", UnitPrice = 97.0000M, UnitsInStock = 29 ,
new Product ProductID = 17, ProductName = "Alice Mutton", Category = "Meat/Poultry", UnitPrice = 39.0000M, UnitsInStock = 0 ,
new Product ProductID = 24, ProductName = "Guaraná Fantástica", Category = "Beverages", UnitPrice = 4.5000M, UnitsInStock = 20 ,
new Product ProductID = 29, ProductName = "Thüringer Rostbratwurst", Category = "Meat/Poultry", UnitPrice = 123.7900M, UnitsInStock = 0 ,
new Product ProductID = 34, ProductName = "Sasquatch Ale", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 111 ,
new Product ProductID = 35, ProductName = "Steeleye Stout", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 20 ,
new Product ProductID = 38, ProductName = "Côte de Blaye", Category = "Beverages", UnitPrice = 263.5000M, UnitsInStock = 17 ,
new Product ProductID = 39, ProductName = "Chartreuse verte", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 69 ,
new Product ProductID = 43, ProductName = "Ipoh Coffee", Category = "Beverages", UnitPrice = 46.0000M, UnitsInStock = 17 ,
new Product ProductID = 53, ProductName = "Perth Pasties", Category = "Meat/Poultry", UnitPrice = 32.8000M, UnitsInStock = 0 ,
new Product ProductID = 54, ProductName = "Tourtière", Category = "Meat/Poultry", UnitPrice = 7.4500M, UnitsInStock = 21 ,
new Product ProductID = 55, ProductName = "Pâté chinois", Category = "Meat/Poultry", UnitPrice = 24.0000M, UnitsInStock = 115 ,
new Product ProductID = 67, ProductName = "Laughing Lumberjack Lager", Category = "Beverages", UnitPrice = 14.0000M, UnitsInStock = 52 ,
new Product ProductID = 70, ProductName = "Outback Lager", Category = "Beverages", UnitPrice = 15.0000M, UnitsInStock = 15 ,
new Product ProductID = 75, ProductName = "Rhönbräu Klosterbier", Category = "Beverages", UnitPrice = 7.7500M, UnitsInStock = 125 ,
new Product ProductID = 76, ProductName = "Lakkalikööri", Category = "Beverages", UnitPrice = 18.0000M, UnitsInStock = 57
;


Here is the solution that I came up with, but I can not compare stock and price.



var category = productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
UnitsInStock = s.Select(s2 => s2.UnitsInStock),
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitsInStock = category.GroupBy(g=> new g.Category, g.UnitsInStock)
.Select(s => new

UnitsInStock = s.Key.UnitsInStock,
Category = s.Key.Category,
UnitPrice = s.Select(s2 => s2.UnitPrice)
);

var unitPrice = unitsInStock.GroupBy(g => new g.Category, g.UnitsInStock, g.UnitPrice)
.Select(s => new

UnitPrice = s.Key.UnitPrice,
Category = s.Key.Category,
UnitsInStock = s.Key.UnitsInStock
);

foreach (var categ in unitPrice)

Console.WriteLine($"Category: categ.Category");

foreach (var stock in categ.UnitsInStock)

Console.WriteLine($"UnitsInStock: stock");

foreach (var price in categ.UnitPrice.SelectMany(s => s))

Console.WriteLine($"UnitPrice: price");




enter image description here
I want stock and price to be in one line



Expected result:



enter image description here



The entire complexity of the task is that it is necessary from the beginning from grouping by Category, then by UnitsInStock, then UnitPrice. And all this output is grouped





This question already has an answer here:



  • Group By Multiple Columns

    12 answers







c# linq






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 19 hours ago

























asked 20 hours ago









Tibomso

1149




1149




marked as duplicate by TaW c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
19 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by TaW c#
Users with the  c# badge can single-handedly close c# questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
19 hours ago


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • what is the desired result? could you provide an example class of the result?
    – Ashkan Mobayen Khiabani
    19 hours ago










  • Not sure the retirement makes sense. In the last group, if you have two products with the same units in stock to group on, then what price do you show?
    – Paddy
    19 hours ago










  • @AshkanMobayenKhiabani, yes ofc
    – Tibomso
    19 hours ago










  • @Tibomso well then Enigmativity's answer is exactly what you want
    – Ashkan Mobayen Khiabani
    19 hours ago











  • @AshkanMobayenKhiabani, yes,but if I understood correctly from his answer, I need to remove my groupings and make only one, although on the task I need to be grouped from the beginning: Category -> UnitsInStock->UnitPrice
    – Tibomso
    19 hours ago
















  • what is the desired result? could you provide an example class of the result?
    – Ashkan Mobayen Khiabani
    19 hours ago










  • Not sure the retirement makes sense. In the last group, if you have two products with the same units in stock to group on, then what price do you show?
    – Paddy
    19 hours ago










  • @AshkanMobayenKhiabani, yes ofc
    – Tibomso
    19 hours ago










  • @Tibomso well then Enigmativity's answer is exactly what you want
    – Ashkan Mobayen Khiabani
    19 hours ago











  • @AshkanMobayenKhiabani, yes,but if I understood correctly from his answer, I need to remove my groupings and make only one, although on the task I need to be grouped from the beginning: Category -> UnitsInStock->UnitPrice
    – Tibomso
    19 hours ago















what is the desired result? could you provide an example class of the result?
– Ashkan Mobayen Khiabani
19 hours ago




what is the desired result? could you provide an example class of the result?
– Ashkan Mobayen Khiabani
19 hours ago












Not sure the retirement makes sense. In the last group, if you have two products with the same units in stock to group on, then what price do you show?
– Paddy
19 hours ago




Not sure the retirement makes sense. In the last group, if you have two products with the same units in stock to group on, then what price do you show?
– Paddy
19 hours ago












@AshkanMobayenKhiabani, yes ofc
– Tibomso
19 hours ago




@AshkanMobayenKhiabani, yes ofc
– Tibomso
19 hours ago












@Tibomso well then Enigmativity's answer is exactly what you want
– Ashkan Mobayen Khiabani
19 hours ago





@Tibomso well then Enigmativity's answer is exactly what you want
– Ashkan Mobayen Khiabani
19 hours ago













@AshkanMobayenKhiabani, yes,but if I understood correctly from his answer, I need to remove my groupings and make only one, although on the task I need to be grouped from the beginning: Category -> UnitsInStock->UnitPrice
– Tibomso
19 hours ago




@AshkanMobayenKhiabani, yes,but if I understood correctly from his answer, I need to remove my groupings and make only one, although on the task I need to be grouped from the beginning: Category -> UnitsInStock->UnitPrice
– Tibomso
19 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I believe this gives you what you want.



var category =
productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
Units = s.Select(s2 => new s2.UnitsInStock, s2.UnitPrice )
);


foreach (var categ in category)

Console.WriteLine($"Category: categ.Category");
foreach (var unit in categ.Units)

Console.WriteLine($"UnitsInStock: unit.UnitsInStock; UnitPrice: unit.UnitPrice");




That gives me:




Category: Beverages
UnitsInStock: 39; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 19.0000
UnitsInStock: 20; UnitPrice: 4.5000
UnitsInStock: 111; UnitPrice: 14.0000
UnitsInStock: 20; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 263.5000
UnitsInStock: 69; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 46.0000
UnitsInStock: 52; UnitPrice: 14.0000
UnitsInStock: 15; UnitPrice: 15.0000
UnitsInStock: 125; UnitPrice: 7.7500
UnitsInStock: 57; UnitPrice: 18.0000
Category: Meat/Poultry
UnitsInStock: 29; UnitPrice: 97.0000
UnitsInStock: 0; UnitPrice: 39.0000
UnitsInStock: 0; UnitPrice: 123.7900
UnitsInStock: 0; UnitPrice: 32.8000
UnitsInStock: 21; UnitPrice: 7.4500
UnitsInStock: 115; UnitPrice: 24.0000





share|improve this answer






















  • Do I understand correctly that my groups unitsInStock and unitPrice are not needed?
    – Tibomso
    19 hours ago










  • That appears to be right based on your desired output.
    – Enigmativity
    19 hours ago










  • Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you
    – Tibomso
    19 hours ago










  • What do you mean by "pay attention to the task itself"?
    – Enigmativity
    19 hours ago










  • I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done?
    – Tibomso
    19 hours ago

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I believe this gives you what you want.



var category =
productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
Units = s.Select(s2 => new s2.UnitsInStock, s2.UnitPrice )
);


foreach (var categ in category)

Console.WriteLine($"Category: categ.Category");
foreach (var unit in categ.Units)

Console.WriteLine($"UnitsInStock: unit.UnitsInStock; UnitPrice: unit.UnitPrice");




That gives me:




Category: Beverages
UnitsInStock: 39; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 19.0000
UnitsInStock: 20; UnitPrice: 4.5000
UnitsInStock: 111; UnitPrice: 14.0000
UnitsInStock: 20; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 263.5000
UnitsInStock: 69; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 46.0000
UnitsInStock: 52; UnitPrice: 14.0000
UnitsInStock: 15; UnitPrice: 15.0000
UnitsInStock: 125; UnitPrice: 7.7500
UnitsInStock: 57; UnitPrice: 18.0000
Category: Meat/Poultry
UnitsInStock: 29; UnitPrice: 97.0000
UnitsInStock: 0; UnitPrice: 39.0000
UnitsInStock: 0; UnitPrice: 123.7900
UnitsInStock: 0; UnitPrice: 32.8000
UnitsInStock: 21; UnitPrice: 7.4500
UnitsInStock: 115; UnitPrice: 24.0000





share|improve this answer






















  • Do I understand correctly that my groups unitsInStock and unitPrice are not needed?
    – Tibomso
    19 hours ago










  • That appears to be right based on your desired output.
    – Enigmativity
    19 hours ago










  • Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you
    – Tibomso
    19 hours ago










  • What do you mean by "pay attention to the task itself"?
    – Enigmativity
    19 hours ago










  • I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done?
    – Tibomso
    19 hours ago














up vote
0
down vote



accepted










I believe this gives you what you want.



var category =
productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
Units = s.Select(s2 => new s2.UnitsInStock, s2.UnitPrice )
);


foreach (var categ in category)

Console.WriteLine($"Category: categ.Category");
foreach (var unit in categ.Units)

Console.WriteLine($"UnitsInStock: unit.UnitsInStock; UnitPrice: unit.UnitPrice");




That gives me:




Category: Beverages
UnitsInStock: 39; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 19.0000
UnitsInStock: 20; UnitPrice: 4.5000
UnitsInStock: 111; UnitPrice: 14.0000
UnitsInStock: 20; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 263.5000
UnitsInStock: 69; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 46.0000
UnitsInStock: 52; UnitPrice: 14.0000
UnitsInStock: 15; UnitPrice: 15.0000
UnitsInStock: 125; UnitPrice: 7.7500
UnitsInStock: 57; UnitPrice: 18.0000
Category: Meat/Poultry
UnitsInStock: 29; UnitPrice: 97.0000
UnitsInStock: 0; UnitPrice: 39.0000
UnitsInStock: 0; UnitPrice: 123.7900
UnitsInStock: 0; UnitPrice: 32.8000
UnitsInStock: 21; UnitPrice: 7.4500
UnitsInStock: 115; UnitPrice: 24.0000





share|improve this answer






















  • Do I understand correctly that my groups unitsInStock and unitPrice are not needed?
    – Tibomso
    19 hours ago










  • That appears to be right based on your desired output.
    – Enigmativity
    19 hours ago










  • Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you
    – Tibomso
    19 hours ago










  • What do you mean by "pay attention to the task itself"?
    – Enigmativity
    19 hours ago










  • I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done?
    – Tibomso
    19 hours ago












up vote
0
down vote



accepted







up vote
0
down vote



accepted






I believe this gives you what you want.



var category =
productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
Units = s.Select(s2 => new s2.UnitsInStock, s2.UnitPrice )
);


foreach (var categ in category)

Console.WriteLine($"Category: categ.Category");
foreach (var unit in categ.Units)

Console.WriteLine($"UnitsInStock: unit.UnitsInStock; UnitPrice: unit.UnitPrice");




That gives me:




Category: Beverages
UnitsInStock: 39; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 19.0000
UnitsInStock: 20; UnitPrice: 4.5000
UnitsInStock: 111; UnitPrice: 14.0000
UnitsInStock: 20; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 263.5000
UnitsInStock: 69; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 46.0000
UnitsInStock: 52; UnitPrice: 14.0000
UnitsInStock: 15; UnitPrice: 15.0000
UnitsInStock: 125; UnitPrice: 7.7500
UnitsInStock: 57; UnitPrice: 18.0000
Category: Meat/Poultry
UnitsInStock: 29; UnitPrice: 97.0000
UnitsInStock: 0; UnitPrice: 39.0000
UnitsInStock: 0; UnitPrice: 123.7900
UnitsInStock: 0; UnitPrice: 32.8000
UnitsInStock: 21; UnitPrice: 7.4500
UnitsInStock: 115; UnitPrice: 24.0000





share|improve this answer














I believe this gives you what you want.



var category =
productList
.GroupBy(g => g.Category)
.Select(s => new

Category = s.Key,
Units = s.Select(s2 => new s2.UnitsInStock, s2.UnitPrice )
);


foreach (var categ in category)

Console.WriteLine($"Category: categ.Category");
foreach (var unit in categ.Units)

Console.WriteLine($"UnitsInStock: unit.UnitsInStock; UnitPrice: unit.UnitPrice");




That gives me:




Category: Beverages
UnitsInStock: 39; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 19.0000
UnitsInStock: 20; UnitPrice: 4.5000
UnitsInStock: 111; UnitPrice: 14.0000
UnitsInStock: 20; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 263.5000
UnitsInStock: 69; UnitPrice: 18.0000
UnitsInStock: 17; UnitPrice: 46.0000
UnitsInStock: 52; UnitPrice: 14.0000
UnitsInStock: 15; UnitPrice: 15.0000
UnitsInStock: 125; UnitPrice: 7.7500
UnitsInStock: 57; UnitPrice: 18.0000
Category: Meat/Poultry
UnitsInStock: 29; UnitPrice: 97.0000
UnitsInStock: 0; UnitPrice: 39.0000
UnitsInStock: 0; UnitPrice: 123.7900
UnitsInStock: 0; UnitPrice: 32.8000
UnitsInStock: 21; UnitPrice: 7.4500
UnitsInStock: 115; UnitPrice: 24.0000






share|improve this answer














share|improve this answer



share|improve this answer








edited 19 hours ago

























answered 19 hours ago









Enigmativity

74.2k763128




74.2k763128











  • Do I understand correctly that my groups unitsInStock and unitPrice are not needed?
    – Tibomso
    19 hours ago










  • That appears to be right based on your desired output.
    – Enigmativity
    19 hours ago










  • Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you
    – Tibomso
    19 hours ago










  • What do you mean by "pay attention to the task itself"?
    – Enigmativity
    19 hours ago










  • I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done?
    – Tibomso
    19 hours ago
















  • Do I understand correctly that my groups unitsInStock and unitPrice are not needed?
    – Tibomso
    19 hours ago










  • That appears to be right based on your desired output.
    – Enigmativity
    19 hours ago










  • Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you
    – Tibomso
    19 hours ago










  • What do you mean by "pay attention to the task itself"?
    – Enigmativity
    19 hours ago










  • I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done?
    – Tibomso
    19 hours ago















Do I understand correctly that my groups unitsInStock and unitPrice are not needed?
– Tibomso
19 hours ago




Do I understand correctly that my groups unitsInStock and unitPrice are not needed?
– Tibomso
19 hours ago












That appears to be right based on your desired output.
– Enigmativity
19 hours ago




That appears to be right based on your desired output.
– Enigmativity
19 hours ago












Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you
– Tibomso
19 hours ago




Yes, I agree with you, but could you pay attention to the task itself, because I began to get confused among all the groups, if it is not difficult for you
– Tibomso
19 hours ago












What do you mean by "pay attention to the task itself"?
– Enigmativity
19 hours ago




What do you mean by "pay attention to the task itself"?
– Enigmativity
19 hours ago












I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done?
– Tibomso
19 hours ago




I meant that the task consists of three groups, can you rely on your answer to conclude that the task is done?
– Tibomso
19 hours ago



Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党