score:1
Accepted answer
something like this :
var p1 = new product(){count=15, name="a", vendor = 1, year = 1990};
var p2 = new product() { count = 20, name = "a", vendor = 2, year = 1990 };
var p3 = new product() { count = 5, name = "a", vendor = 4, year = 1998 };
var p4 = new product() { count = 15, name = "b", vendor = 1, year = 1995 };
var p5 = new product() { count = 2, name = "b", vendor = 1, year = 1995 };
var inputlist = new[] {p1, p2, p3, p4, p5};
var newstructure = inputlist
.groupby( p => new { p.name, p.year } )
.select( g => new { g.key.name, g.key.year, vendorcount = g.todictionary(p => p.vendor, p => p.count) }
);
although i don't think you can use vendor as a dictionary key, because they are not unique as it stands
score:2
use this if the combination of (name, year, vendor) is unique:
inputlist.groupby(x => new { x.name, x.year })
.select(x => new
{
productname = x.key.name,
originyear = x.key.year,
vendor_count = x.todictionary(y => y.vendor
y => y.count)
})
.orderby(x => x.productname).thenby(x => x.originyear);
use this if it isn't unique:
inputlist.groupby(x => new { x.name, x.year })
.select(x => new
{
productname = x.key.name,
originyear = x.key.year,
vendor_count = x.groupby(y => y.vendor)
.todictionary(y => y.key,
y => y.sum(z => z.count))
})
.orderby(x => x.productname).thenby(x => x.originyear);
Source: stackoverflow.com
Related Query
- c# Linq or code to extract groups from a single list of source data
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How does linq actually execute the code to retrieve data from the data source?
- LINQ grouping and aggregating data with conditions
- LINQ Source Code Available
- LINQ Grouping Data Twice
- Merge duplicate data without affecting others in LINQ code
- Advanced LINQ Grouping and Projection Query for Hierarchical Data (EF 4.0 + LINQ + ASP.NET MVC + HighCharts)
- How do I write a LINQ query that inverts the grouping of a hierarchical data source?
- Use a linq query as microsoft local report Data Source (WinForms)
- Grouping Data with LINQ Not Working
- At what point is a LINQ data source determined?
- Grouping data with a LINQ query
- creating Linq to sqlite dbml from DbLinq source code
- Grouping data between ranges using LINQ in C#
- read icollection data using LINQ in C# code
- Linq to sql as object data source - designer problem with partial classes
- C# LINQ How to get a data source from a db?
- Using LINQ query result for data source for GridControl c#
- How to swap the data source associated with a Linq query?
- Grouping hierarchical data with LINQ
- Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?
- LINQ entity data model generated code error - The type 'DBContexts.Category' already contains a definition for 'ID'
- ASP.Net LINQ data source error is ListView
- Linq Query Grouping Data table using two columns
- source code for LINQ 101 samples
- Linq query null check in data source
- LINQ - changing Data Source (and the LINQ Provider) at runtime in C#
- Data binding access in xaml vs in code behind - Linq to XML?
- Grouping data via LinQ in Powershell
More Query from same tag
- Efficient implementation of a "ThenBy" sort
- Sort array on one variable that can hold multiple values
- LINQ: How can I centralize this condition .Where(job => !job.IsDeleted && !job.IsPrivate)
- c#: IEnumerable not work outside for loop
- The cast to value type 'System.Single' failed because the materialized value is null for stored procedure
- LINQ to DataSet, DataTable.AsEnumerable() not recognized
- Linq, array.Contains() generating exception: Only primitive types ('such as Int32, String, and Guid') are supported in this context
- C# LINQ DataTime List
- Remove XML Tag using linq c#
- DataTable joins c# - joins on datatable
- LINQ - Combine multiple lists to form a new list and align them by key?
- EF Core SingleOrDefaultAsync compile time return type error
- Get column value from gridview via LINQ
- How do you use Models in a master page with ASP.NET MVC?
- How should I correctly join two tables so that I can access a property in one of the entities?
- How to fetch records which satisfy the query in wp8
- How to show enum value in multiple join scenarios?
- How to use where inside a find for complex search queries?
- How to search a class within a namespace by the value of an attribute and return an object?
- Convert Jagged Array to IEnumerable<KeyValuePair<int,int>>?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Merging two datatable in memory and grouping them to get sum of columns.Using linq but kind of lost here
- Joining a CSV file to query results
- Linq result to ViewData and show result in PartialView
- Casting linq query result to enum
- best way to cache lookup tables in memory
- Strange behaviour in linq c# under delayed execution
- LINQ query doesn't return the single value
- The cast to value type 'Decimal' failed because the materialized value is null
- LINQ left join with nullable values