Include all fields after grouping and summing using c# linqGroup by in LINQCan I return the 'id' field after a LINQ insert?Update all objects in a collection using LINQConcat all strings inside a List<string> using LINQWhat is a method group in C#?How to loop through all enum values in C#?C# Linq Group By on multiple columnsGroup by in LINQLINQ: Not Any vs All Don'tC# LINQ find duplicates in ListUse LINQ to group by one field, sum based on group AND store on Dictionary
Start from ones
Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?
Irish Snap: Variant Rules
Using `With[...]` with a list specification as a variable
Can realistic planetary invasion have any meaningful strategy?
If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?
Can pay be witheld for hours cleaning up after closing time?
Why can't an Airbus A330 dump fuel in an emergency?
LeetCode: Pascal's Triangle C#
In an emergency, how do I find and share my position?
How to draw a cube that can be inscribed within a right circular cone?
Can't stopover at Sapporo when going from Asahikawa to Chitose airport?
Did a flight controller ever answer Flight with a no-go?
How much code would a codegolf golf if a codegolf could golf code?
Avoiding racist tropes in fantasy
Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?
In the MCU, why does Mjölnir retain its enchantments after Ragnarok?
What is this symbol: semicircles facing eachother
Earth rotation discrepancy
How to stop this icon from appearing on the taskbar?
Science fiction short story where aliens contact a drunk about Earth's impending destruction
Why don't launcher rockets ordinarily have names?
Sun setting in East!
How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?
Include all fields after grouping and summing using c# linq
Group by in LINQCan I return the 'id' field after a LINQ insert?Update all objects in a collection using LINQConcat all strings inside a List<string> using LINQWhat is a method group in C#?How to loop through all enum values in C#?C# Linq Group By on multiple columnsGroup by in LINQLINQ: Not Any vs All Don'tC# LINQ find duplicates in ListUse LINQ to group by one field, sum based on group AND store on Dictionary
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am basically trying to group the allocationsGrouped list by productname and summing the Emv field. If you below i have assigned the grouping to var a. Unfortunately the after grouping it only contains ProductName and EmvSum as thats the only two fields added in the Select statement
I need all the fields below listed in the foreach (var ac in allocationsGrouped). I would need to loop through a and not allocationsGrouped. So it will be foreach (var ac in a). How do I get the other fields after grouping and summing.
private static void CreateHierarchy(string manStratName, IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationsGrouped, List<FirmWideAllocationsViewModel> result)
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
);
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in allocationsGrouped)
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(ac.PRODUCT_NAME + "#" + ac.MANAGER_FUND_ID + ac.PRODUCT_ID + ac.EMV);
item2.FirmID = ac.FIRM_ID;
item2.FirmName = ac.FIRM_NAME;
item2.ManagerStrategyID = ac.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = ac.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = ac.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = ac.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = ac.MANAGER_FUND_ID;
item2.ManagerFundName = ac.MANAGER_FUND_NAME;
item2.Nav = ac.NAV;
item2.EvalDate = ac.EVAL_DATE.HasValue ? ac.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = ac.PRODUCT_ID;
item2.ProductName = ac.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)ac.UsdEmv);
item2.GroupPercent = ac.GroupPercent;
item2.WeightWithEq = ac.WEIGHT_WITH_EQ;
result.Add(item2);
Added the count and filter on a specific product that has duplicate on the proposed solution . The outer count shows one while the inner count shows 2
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP")
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
var b = a;
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in b)
var count = b.Count();
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
var count1 = ac.Items.Count();
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(elem.PRODUCT_NAME);
item2.FirmID = elem.FIRM_ID;
item2.FirmName = elem.FIRM_NAME;
item2.ManagerStrategyID = elem.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = elem.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = elem.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = elem.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = elem.MANAGER_FUND_ID;
item2.ManagerFundName = elem.MANAGER_FUND_NAME;
item2.Nav = elem.NAV;
item2.EvalDate = elem.EVAL_DATE.HasValue ? elem.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = elem.PRODUCT_ID;
item2.ProductName = elem.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)elem.UsdEmv);
item2.GroupPercent = elem.GroupPercent;
item2.WeightWithEq = elem.WEIGHT_WITH_EQ;
result.Add(item2);
return result;
c# linq
|
show 4 more comments
I am basically trying to group the allocationsGrouped list by productname and summing the Emv field. If you below i have assigned the grouping to var a. Unfortunately the after grouping it only contains ProductName and EmvSum as thats the only two fields added in the Select statement
I need all the fields below listed in the foreach (var ac in allocationsGrouped). I would need to loop through a and not allocationsGrouped. So it will be foreach (var ac in a). How do I get the other fields after grouping and summing.
private static void CreateHierarchy(string manStratName, IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationsGrouped, List<FirmWideAllocationsViewModel> result)
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
);
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in allocationsGrouped)
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(ac.PRODUCT_NAME + "#" + ac.MANAGER_FUND_ID + ac.PRODUCT_ID + ac.EMV);
item2.FirmID = ac.FIRM_ID;
item2.FirmName = ac.FIRM_NAME;
item2.ManagerStrategyID = ac.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = ac.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = ac.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = ac.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = ac.MANAGER_FUND_ID;
item2.ManagerFundName = ac.MANAGER_FUND_NAME;
item2.Nav = ac.NAV;
item2.EvalDate = ac.EVAL_DATE.HasValue ? ac.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = ac.PRODUCT_ID;
item2.ProductName = ac.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)ac.UsdEmv);
item2.GroupPercent = ac.GroupPercent;
item2.WeightWithEq = ac.WEIGHT_WITH_EQ;
result.Add(item2);
Added the count and filter on a specific product that has duplicate on the proposed solution . The outer count shows one while the inner count shows 2
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP")
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
var b = a;
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in b)
var count = b.Count();
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
var count1 = ac.Items.Count();
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(elem.PRODUCT_NAME);
item2.FirmID = elem.FIRM_ID;
item2.FirmName = elem.FIRM_NAME;
item2.ManagerStrategyID = elem.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = elem.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = elem.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = elem.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = elem.MANAGER_FUND_ID;
item2.ManagerFundName = elem.MANAGER_FUND_NAME;
item2.Nav = elem.NAV;
item2.EvalDate = elem.EVAL_DATE.HasValue ? elem.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = elem.PRODUCT_ID;
item2.ProductName = elem.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)elem.UsdEmv);
item2.GroupPercent = elem.GroupPercent;
item2.WeightWithEq = elem.WEIGHT_WITH_EQ;
result.Add(item2);
return result;
c# linq
A group is a two dimensional array [group][items]. So following will get everything : .Select(group => new ProductName = group.Key, EmvSum = group.Sum(x => x.EMV), group = group ); You would have to enumerate through the group using group.Select(z => ....)
– jdweng
Mar 27 at 16:53
Somthing like this? In that example you will get a list ofkey
+Cars
, cars will have all properties.
– nilsK
Mar 27 at 16:54
Did you mean var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME)) .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), group ); var b = a.Select(x => x.group);
– Tom
Mar 27 at 17:11
b is still not containing the fields
– Tom
Mar 27 at 17:11
In your codevar count = b.Count()
is the number of distinct product names, whereasvar count1 = ac.Items.Count()
, is the number of products with the same product names. Why would you expect them to match?
– Vikhram
Mar 27 at 19:42
|
show 4 more comments
I am basically trying to group the allocationsGrouped list by productname and summing the Emv field. If you below i have assigned the grouping to var a. Unfortunately the after grouping it only contains ProductName and EmvSum as thats the only two fields added in the Select statement
I need all the fields below listed in the foreach (var ac in allocationsGrouped). I would need to loop through a and not allocationsGrouped. So it will be foreach (var ac in a). How do I get the other fields after grouping and summing.
private static void CreateHierarchy(string manStratName, IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationsGrouped, List<FirmWideAllocationsViewModel> result)
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
);
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in allocationsGrouped)
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(ac.PRODUCT_NAME + "#" + ac.MANAGER_FUND_ID + ac.PRODUCT_ID + ac.EMV);
item2.FirmID = ac.FIRM_ID;
item2.FirmName = ac.FIRM_NAME;
item2.ManagerStrategyID = ac.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = ac.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = ac.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = ac.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = ac.MANAGER_FUND_ID;
item2.ManagerFundName = ac.MANAGER_FUND_NAME;
item2.Nav = ac.NAV;
item2.EvalDate = ac.EVAL_DATE.HasValue ? ac.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = ac.PRODUCT_ID;
item2.ProductName = ac.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)ac.UsdEmv);
item2.GroupPercent = ac.GroupPercent;
item2.WeightWithEq = ac.WEIGHT_WITH_EQ;
result.Add(item2);
Added the count and filter on a specific product that has duplicate on the proposed solution . The outer count shows one while the inner count shows 2
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP")
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
var b = a;
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in b)
var count = b.Count();
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
var count1 = ac.Items.Count();
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(elem.PRODUCT_NAME);
item2.FirmID = elem.FIRM_ID;
item2.FirmName = elem.FIRM_NAME;
item2.ManagerStrategyID = elem.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = elem.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = elem.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = elem.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = elem.MANAGER_FUND_ID;
item2.ManagerFundName = elem.MANAGER_FUND_NAME;
item2.Nav = elem.NAV;
item2.EvalDate = elem.EVAL_DATE.HasValue ? elem.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = elem.PRODUCT_ID;
item2.ProductName = elem.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)elem.UsdEmv);
item2.GroupPercent = elem.GroupPercent;
item2.WeightWithEq = elem.WEIGHT_WITH_EQ;
result.Add(item2);
return result;
c# linq
I am basically trying to group the allocationsGrouped list by productname and summing the Emv field. If you below i have assigned the grouping to var a. Unfortunately the after grouping it only contains ProductName and EmvSum as thats the only two fields added in the Select statement
I need all the fields below listed in the foreach (var ac in allocationsGrouped). I would need to loop through a and not allocationsGrouped. So it will be foreach (var ac in a). How do I get the other fields after grouping and summing.
private static void CreateHierarchy(string manStratName, IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationsGrouped, List<FirmWideAllocationsViewModel> result)
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
);
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in allocationsGrouped)
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(ac.PRODUCT_NAME + "#" + ac.MANAGER_FUND_ID + ac.PRODUCT_ID + ac.EMV);
item2.FirmID = ac.FIRM_ID;
item2.FirmName = ac.FIRM_NAME;
item2.ManagerStrategyID = ac.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = ac.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = ac.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = ac.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = ac.MANAGER_FUND_ID;
item2.ManagerFundName = ac.MANAGER_FUND_NAME;
item2.Nav = ac.NAV;
item2.EvalDate = ac.EVAL_DATE.HasValue ? ac.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = ac.PRODUCT_ID;
item2.ProductName = ac.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)ac.UsdEmv);
item2.GroupPercent = ac.GroupPercent;
item2.WeightWithEq = ac.WEIGHT_WITH_EQ;
result.Add(item2);
Added the count and filter on a specific product that has duplicate on the proposed solution . The outer count shows one while the inner count shows 2
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP")
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
var b = a;
var item = new FirmWideAllocationsViewModel();
item.Hierarchy = new List<string>();
item.Hierarchy.Add(manStratName);
result.Add(item);
foreach (var ac in b)
var count = b.Count();
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
var count1 = ac.Items.Count();
var item2 = new FirmWideAllocationsViewModel();
item2.Hierarchy = new List<string>();
item2.Hierarchy.Add(manStratName);
item2.Hierarchy.Add(elem.PRODUCT_NAME);
item2.FirmID = elem.FIRM_ID;
item2.FirmName = elem.FIRM_NAME;
item2.ManagerStrategyID = elem.MANAGER_STRATEGY_ID;
item2.ManagerStrategyName = elem.MANAGER_STRATEGY_NAME;
item2.ManagerAccountClassID = elem.MANAGER_ACCOUNTING_CLASS_ID;
item2.ManagerAccountingClassName = elem.MANAGER_ACCOUNTING_CLASS_NAME;
item2.ManagerFundID = elem.MANAGER_FUND_ID;
item2.ManagerFundName = elem.MANAGER_FUND_NAME;
item2.Nav = elem.NAV;
item2.EvalDate = elem.EVAL_DATE.HasValue ? elem.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
item2.ProductID = elem.PRODUCT_ID;
item2.ProductName = elem.PRODUCT_NAME;
item2.UsdEmv = Math.Round((decimal)elem.UsdEmv);
item2.GroupPercent = elem.GroupPercent;
item2.WeightWithEq = elem.WEIGHT_WITH_EQ;
result.Add(item2);
return result;
c# linq
c# linq
edited Mar 27 at 18:48
Tom
asked Mar 27 at 16:47
TomTom
59510 gold badges39 silver badges102 bronze badges
59510 gold badges39 silver badges102 bronze badges
A group is a two dimensional array [group][items]. So following will get everything : .Select(group => new ProductName = group.Key, EmvSum = group.Sum(x => x.EMV), group = group ); You would have to enumerate through the group using group.Select(z => ....)
– jdweng
Mar 27 at 16:53
Somthing like this? In that example you will get a list ofkey
+Cars
, cars will have all properties.
– nilsK
Mar 27 at 16:54
Did you mean var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME)) .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), group ); var b = a.Select(x => x.group);
– Tom
Mar 27 at 17:11
b is still not containing the fields
– Tom
Mar 27 at 17:11
In your codevar count = b.Count()
is the number of distinct product names, whereasvar count1 = ac.Items.Count()
, is the number of products with the same product names. Why would you expect them to match?
– Vikhram
Mar 27 at 19:42
|
show 4 more comments
A group is a two dimensional array [group][items]. So following will get everything : .Select(group => new ProductName = group.Key, EmvSum = group.Sum(x => x.EMV), group = group ); You would have to enumerate through the group using group.Select(z => ....)
– jdweng
Mar 27 at 16:53
Somthing like this? In that example you will get a list ofkey
+Cars
, cars will have all properties.
– nilsK
Mar 27 at 16:54
Did you mean var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME)) .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), group ); var b = a.Select(x => x.group);
– Tom
Mar 27 at 17:11
b is still not containing the fields
– Tom
Mar 27 at 17:11
In your codevar count = b.Count()
is the number of distinct product names, whereasvar count1 = ac.Items.Count()
, is the number of products with the same product names. Why would you expect them to match?
– Vikhram
Mar 27 at 19:42
A group is a two dimensional array [group][items]. So following will get everything : .Select(group => new ProductName = group.Key, EmvSum = group.Sum(x => x.EMV), group = group ); You would have to enumerate through the group using group.Select(z => ....)
– jdweng
Mar 27 at 16:53
A group is a two dimensional array [group][items]. So following will get everything : .Select(group => new ProductName = group.Key, EmvSum = group.Sum(x => x.EMV), group = group ); You would have to enumerate through the group using group.Select(z => ....)
– jdweng
Mar 27 at 16:53
Somthing like this? In that example you will get a list of
key
+ Cars
, cars will have all properties.– nilsK
Mar 27 at 16:54
Somthing like this? In that example you will get a list of
key
+ Cars
, cars will have all properties.– nilsK
Mar 27 at 16:54
Did you mean var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME)) .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), group ); var b = a.Select(x => x.group);
– Tom
Mar 27 at 17:11
Did you mean var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME)) .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), group ); var b = a.Select(x => x.group);
– Tom
Mar 27 at 17:11
b is still not containing the fields
– Tom
Mar 27 at 17:11
b is still not containing the fields
– Tom
Mar 27 at 17:11
In your code
var count = b.Count()
is the number of distinct product names, whereas var count1 = ac.Items.Count()
, is the number of products with the same product names. Why would you expect them to match?– Vikhram
Mar 27 at 19:42
In your code
var count = b.Count()
is the number of distinct product names, whereas var count1 = ac.Items.Count()
, is the number of products with the same product names. Why would you expect them to match?– Vikhram
Mar 27 at 19:42
|
show 4 more comments
1 Answer
1
active
oldest
votes
The IGrouping<TKey, TElement>
type is an IEnumerable<TElement>
. What it means is that you just need to select the group
for further use. So the relevant part of teh code would be
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
Notice the Items = group
in the Select
And you can access the grouped items now as below
foreach (var ac in a)
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
// elem is your FIRMWIDE_MANAGER_ALLOCATION in this group
TEST
You can test this using the following code
var allocationsGrouped = new List<Product>()
new Product PRODUCT_NAME = "T1", EMV = 1 ,
new Product PRODUCT_NAME = "T2", EMV = 2 ,
new Product PRODUCT_NAME = "T1", EMV = 3 ,
new Product PRODUCT_NAME = "T2", EMV = 4 ,
new Product PRODUCT_NAME = "T1", EMV = 5 ,
;
You will notice that, this will structure the data as below
"a":
[
[
"ProductName": "T1"
"EmvSum": 9,
"Items":
[
["EMV": 1, "PRODUCT_NAME": "T1"],
["EMV": 3, "PRODUCT_NAME": "T1"],
["EMV": 5, "PRODUCT_NAME": "T1"]
],
],
[
"ProductName": "T2"
"EmvSum": 6,
"Items":
[
["EMV": 2, "PRODUCT_NAME": "T2"],
["EMV": 4, "PRODUCT_NAME": "T2"],
],
]
]
Hi Vikram, thanks but there is one issue here. Items does not contain the filtered list. I was expecting the Items contains the same count as the summed one
– Tom
Mar 27 at 18:18
@Tom It works for me. Let me post you some test
– Vikhram
Mar 27 at 18:23
I had tried to check the duplicate product like this and got two instances in the items var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP") .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), Items = group );
– Tom
Mar 27 at 18:38
Updated the post with my findings
– Tom
Mar 27 at 18:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382506%2finclude-all-fields-after-grouping-and-summing-using-c-sharp-linq%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The IGrouping<TKey, TElement>
type is an IEnumerable<TElement>
. What it means is that you just need to select the group
for further use. So the relevant part of teh code would be
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
Notice the Items = group
in the Select
And you can access the grouped items now as below
foreach (var ac in a)
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
// elem is your FIRMWIDE_MANAGER_ALLOCATION in this group
TEST
You can test this using the following code
var allocationsGrouped = new List<Product>()
new Product PRODUCT_NAME = "T1", EMV = 1 ,
new Product PRODUCT_NAME = "T2", EMV = 2 ,
new Product PRODUCT_NAME = "T1", EMV = 3 ,
new Product PRODUCT_NAME = "T2", EMV = 4 ,
new Product PRODUCT_NAME = "T1", EMV = 5 ,
;
You will notice that, this will structure the data as below
"a":
[
[
"ProductName": "T1"
"EmvSum": 9,
"Items":
[
["EMV": 1, "PRODUCT_NAME": "T1"],
["EMV": 3, "PRODUCT_NAME": "T1"],
["EMV": 5, "PRODUCT_NAME": "T1"]
],
],
[
"ProductName": "T2"
"EmvSum": 6,
"Items":
[
["EMV": 2, "PRODUCT_NAME": "T2"],
["EMV": 4, "PRODUCT_NAME": "T2"],
],
]
]
Hi Vikram, thanks but there is one issue here. Items does not contain the filtered list. I was expecting the Items contains the same count as the summed one
– Tom
Mar 27 at 18:18
@Tom It works for me. Let me post you some test
– Vikhram
Mar 27 at 18:23
I had tried to check the duplicate product like this and got two instances in the items var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP") .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), Items = group );
– Tom
Mar 27 at 18:38
Updated the post with my findings
– Tom
Mar 27 at 18:48
add a comment |
The IGrouping<TKey, TElement>
type is an IEnumerable<TElement>
. What it means is that you just need to select the group
for further use. So the relevant part of teh code would be
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
Notice the Items = group
in the Select
And you can access the grouped items now as below
foreach (var ac in a)
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
// elem is your FIRMWIDE_MANAGER_ALLOCATION in this group
TEST
You can test this using the following code
var allocationsGrouped = new List<Product>()
new Product PRODUCT_NAME = "T1", EMV = 1 ,
new Product PRODUCT_NAME = "T2", EMV = 2 ,
new Product PRODUCT_NAME = "T1", EMV = 3 ,
new Product PRODUCT_NAME = "T2", EMV = 4 ,
new Product PRODUCT_NAME = "T1", EMV = 5 ,
;
You will notice that, this will structure the data as below
"a":
[
[
"ProductName": "T1"
"EmvSum": 9,
"Items":
[
["EMV": 1, "PRODUCT_NAME": "T1"],
["EMV": 3, "PRODUCT_NAME": "T1"],
["EMV": 5, "PRODUCT_NAME": "T1"]
],
],
[
"ProductName": "T2"
"EmvSum": 6,
"Items":
[
["EMV": 2, "PRODUCT_NAME": "T2"],
["EMV": 4, "PRODUCT_NAME": "T2"],
],
]
]
Hi Vikram, thanks but there is one issue here. Items does not contain the filtered list. I was expecting the Items contains the same count as the summed one
– Tom
Mar 27 at 18:18
@Tom It works for me. Let me post you some test
– Vikhram
Mar 27 at 18:23
I had tried to check the duplicate product like this and got two instances in the items var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP") .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), Items = group );
– Tom
Mar 27 at 18:38
Updated the post with my findings
– Tom
Mar 27 at 18:48
add a comment |
The IGrouping<TKey, TElement>
type is an IEnumerable<TElement>
. What it means is that you just need to select the group
for further use. So the relevant part of teh code would be
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
Notice the Items = group
in the Select
And you can access the grouped items now as below
foreach (var ac in a)
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
// elem is your FIRMWIDE_MANAGER_ALLOCATION in this group
TEST
You can test this using the following code
var allocationsGrouped = new List<Product>()
new Product PRODUCT_NAME = "T1", EMV = 1 ,
new Product PRODUCT_NAME = "T2", EMV = 2 ,
new Product PRODUCT_NAME = "T1", EMV = 3 ,
new Product PRODUCT_NAME = "T2", EMV = 4 ,
new Product PRODUCT_NAME = "T1", EMV = 5 ,
;
You will notice that, this will structure the data as below
"a":
[
[
"ProductName": "T1"
"EmvSum": 9,
"Items":
[
["EMV": 1, "PRODUCT_NAME": "T1"],
["EMV": 3, "PRODUCT_NAME": "T1"],
["EMV": 5, "PRODUCT_NAME": "T1"]
],
],
[
"ProductName": "T2"
"EmvSum": 6,
"Items":
[
["EMV": 2, "PRODUCT_NAME": "T2"],
["EMV": 4, "PRODUCT_NAME": "T2"],
],
]
]
The IGrouping<TKey, TElement>
type is an IEnumerable<TElement>
. What it means is that you just need to select the group
for further use. So the relevant part of teh code would be
var a = allocationsGrouped
.Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME))
.GroupBy(product => product.PRODUCT_NAME)
.Select(group => new
ProductName = group.Key, // this is the value you grouped on - the ProductName
EmvSum = group.Sum(x => x.EMV),
Items = group
);
Notice the Items = group
in the Select
And you can access the grouped items now as below
foreach (var ac in a)
var productName = ac.ProductName;
var emvSum = ac.EmvSum;
foreach (var elem in ac.Items)
// elem is your FIRMWIDE_MANAGER_ALLOCATION in this group
TEST
You can test this using the following code
var allocationsGrouped = new List<Product>()
new Product PRODUCT_NAME = "T1", EMV = 1 ,
new Product PRODUCT_NAME = "T2", EMV = 2 ,
new Product PRODUCT_NAME = "T1", EMV = 3 ,
new Product PRODUCT_NAME = "T2", EMV = 4 ,
new Product PRODUCT_NAME = "T1", EMV = 5 ,
;
You will notice that, this will structure the data as below
"a":
[
[
"ProductName": "T1"
"EmvSum": 9,
"Items":
[
["EMV": 1, "PRODUCT_NAME": "T1"],
["EMV": 3, "PRODUCT_NAME": "T1"],
["EMV": 5, "PRODUCT_NAME": "T1"]
],
],
[
"ProductName": "T2"
"EmvSum": 6,
"Items":
[
["EMV": 2, "PRODUCT_NAME": "T2"],
["EMV": 4, "PRODUCT_NAME": "T2"],
],
]
]
edited Mar 27 at 18:29
answered Mar 27 at 17:18
VikhramVikhram
3,4491 gold badge8 silver badges26 bronze badges
3,4491 gold badge8 silver badges26 bronze badges
Hi Vikram, thanks but there is one issue here. Items does not contain the filtered list. I was expecting the Items contains the same count as the summed one
– Tom
Mar 27 at 18:18
@Tom It works for me. Let me post you some test
– Vikhram
Mar 27 at 18:23
I had tried to check the duplicate product like this and got two instances in the items var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP") .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), Items = group );
– Tom
Mar 27 at 18:38
Updated the post with my findings
– Tom
Mar 27 at 18:48
add a comment |
Hi Vikram, thanks but there is one issue here. Items does not contain the filtered list. I was expecting the Items contains the same count as the summed one
– Tom
Mar 27 at 18:18
@Tom It works for me. Let me post you some test
– Vikhram
Mar 27 at 18:23
I had tried to check the duplicate product like this and got two instances in the items var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP") .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), Items = group );
– Tom
Mar 27 at 18:38
Updated the post with my findings
– Tom
Mar 27 at 18:48
Hi Vikram, thanks but there is one issue here. Items does not contain the filtered list. I was expecting the Items contains the same count as the summed one
– Tom
Mar 27 at 18:18
Hi Vikram, thanks but there is one issue here. Items does not contain the filtered list. I was expecting the Items contains the same count as the summed one
– Tom
Mar 27 at 18:18
@Tom It works for me. Let me post you some test
– Vikhram
Mar 27 at 18:23
@Tom It works for me. Let me post you some test
– Vikhram
Mar 27 at 18:23
I had tried to check the duplicate product like this and got two instances in the items var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP") .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), Items = group );
– Tom
Mar 27 at 18:38
I had tried to check the duplicate product like this and got two instances in the items var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME) && product.PRODUCT_NAME == "EnTrust Global Activist Fund LP") .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), Items = group );
– Tom
Mar 27 at 18:38
Updated the post with my findings
– Tom
Mar 27 at 18:48
Updated the post with my findings
– Tom
Mar 27 at 18:48
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382506%2finclude-all-fields-after-grouping-and-summing-using-c-sharp-linq%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
A group is a two dimensional array [group][items]. So following will get everything : .Select(group => new ProductName = group.Key, EmvSum = group.Sum(x => x.EMV), group = group ); You would have to enumerate through the group using group.Select(z => ....)
– jdweng
Mar 27 at 16:53
Somthing like this? In that example you will get a list of
key
+Cars
, cars will have all properties.– nilsK
Mar 27 at 16:54
Did you mean var a = allocationsGrouped .Where(product => !string.IsNullOrEmpty(product.PRODUCT_NAME)) .GroupBy(product => product.PRODUCT_NAME) .Select(group => new ProductName = group.Key, // this is the value you grouped on - the ProductName EmvSum = group.Sum(x => x.EMV), group ); var b = a.Select(x => x.group);
– Tom
Mar 27 at 17:11
b is still not containing the fields
– Tom
Mar 27 at 17:11
In your code
var count = b.Count()
is the number of distinct product names, whereasvar count1 = ac.Items.Count()
, is the number of products with the same product names. Why would you expect them to match?– Vikhram
Mar 27 at 19:42