score:2
Accepted answer
There can be multiple Values for each column, and there can be multiple columns for each Title.
That means that you have to repeat the first step and GroupBy
again by columns. You also should use your types that you already implemented for this. Don't use anonymous types:
IEnumerable<MyClass> temp = records
.GroupBy(x => x.Title)
.Select(y => new MyClass
{
Title = y.Key,
Columns = y.GroupBy(x => x.Column)
.Select( c => new Column
{
Column_ = c.Key,
Values = c.Select(v => new Value
{
Value_ = v.Value
}).ToList()
}).ToList()
});
temp.Dump();
Result (from the LINQPad Dump):
PS. I changed the names of the properties, because the compiler does not allow them to be named exactly as the class is.
class Column
{
public string Column_ { get; set; }
public List<Value> Values { get; set; }
}
class Value
{
public string Value_ { get; set; }
}
Source: stackoverflow.com
Related Articles
- Read CSV file to nested objects
- c# code to read xml file values
- How do I get a list of all properties including those within nested objects within a class file
- How to flatten nested objects with linq expression
- Query an XML file containing nested elements using LINQPad?
- Sort a list and all its nested objects using LINQ
- Generate POCO objects from xml file
- Calling fellow code nerds - Alternatives to Nested Loops?
- read xml file using linq
- How to read File names recursively from subfolder using LINQ
- How to read XML file using System.IO.Stream with LINQ
- Need a linq to objects query for a nested collection
- How to read from a Text File Faster/Smarter?
- Best way to read csv file in C# to improve time efficiency
- Create a nested list of items from objects with a parent reference
- How to read XML file in c#?
- LINQ Source Code Available
- Read text file into dictionary without duplicates
- HashSet or Distinct to read distinct values of property in List<> of objects
- .NET 4 Code Contracts: "requires unproven: source != null"
- How to read XML file to a Dictionary<String,List<String>> with empty strings for null elements
- Simple Way to Read Integers from a File
- Grouping nested objects using List(T).GroupBy()
- AutoMapper ProjectTo: Not working with nested objects
- How to read xml file having different hierarchy in .net
- How can I read a tab delimited file into a multidimensional array using Linq?
- LINQ to Entities null reference on Union with nested objects
- Read text data from file using LINQ
- Read first 10 line from file using LINQ
- C# .Net 3.5 Code to replace a file extension using LINQ
- How do I Aggregate multiple IEnumerables of T
- how to group by parent and apply aggregate function on its childeren in LinQ Lambda
- Linq put Include inside select new
- LINQ to SQL updates
- Copy a list of objects to another typed list in one line of code
- How to use one Column of Sql query into another sql query inside C# Code
- linq-like sum function in python
- Calling ToList() on Linq Select
- LINQ Contains() alternatives
- How can I use Linq in a T4 template?
- How to Flatten Table or Concatenate Rows?
- XML LINQ: How to use select with where?
- LINQ to SQL - Sql tuning
- Can I compress an if statement using linq in C#?
- Converting string array to double array
- Linq: select objects where list contains properties that must be in another list
- Linq - Order by number then letters
- Group By Multiple Columns - LINQ
- Converting nested foreach loops to LINQ
- UPDATE one to one relationship EntityState.Modified .net