score:2
Accepted answer
If you don't need Console.WriteLine(...)
your code can be summarized with LINQ :
List<EmployeeTelDir> telDir = (from empl in lclEmployees
group empl by empl.Name into employeeGroup
select new EmployeeTelDir
{
Name = employeeGroup.Key,
Phone = (from employee in employeeGroup
select employee.Phone).ToList() // The ToList() is the Holy Grail of the LINQ queries
}).ToList();
For the inverted operation:
List<EmployeeLocalRegister> inverse = (from employeeTelDir in telDir
from phone in employeeTelDir.Phone // Doing 2 from ... in ... successively corresponds to the SelectMany() LINQ method
select new EmployeeLocalRegister
{
Name = employeeTelDir.Name,
Phone = phone
}).ToList();
score:0
The lambda way:
var telDir = lclEmployees.GroupBy(e => e.Name,
(n, p) => new EmployeeTelDir
{
Name = n,
Phone = p.Select(k => k.Phone).ToList()
}).ToList();
Source: stackoverflow.com
Related Articles
- What would be the Linq code for this grouping using VB.NET 2008?
- Convert string[] to int[] in one line of code using LINQ
- Need help in a grouping using LINQ in C#
- Grouping by hour using LINQ
- Left outer join using LINQ -- understanding the code
- How to reuse a linq expression for 'Where' when using multiple source tables
- Using LINQ how do I have a grouping by a "calculated field"
- Avoiding code repetition when using LINQ
- Using LINQ to delete an element from a ObservableCollection Source
- Using strict types in linq grouping query
- Grouping and multiple orderings using Linq to Entities
- Grouping using LINQ
- LINQ Source Code Available
- How can I write the following code more elegantly using LINQ query syntax?
- How can I code an outer join using LINQ and EF6?
- Using Linq for grouping
- Grouping relatively close values using LINQ or loop
- C# .Net 3.5 Code to replace a file extension using LINQ
- Trying to understand LINQ code using c#
- Retrieve bool result by using LinQ code
- calculate the total time grouping by date and id using linq
- 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
- How to do double grouping using LINQ
- Using Linq Grouping how to I order by specific Group then remaining Groups descending
- counting Movie by grouping alphabetically using linq
- Linq sub query when using a repository pattern with EF code first
- Create Dictionary(of Dictionary) using Grouping in Linq
- Using LINQ query result for data source for GridControl c#
- SQL Query to LINQ (use join with two keys)
- Trying to convert sql to linq with subquery
- Can I use LINQ to check if objects in a list have a unique ID?
- Create columns in Linq By using Looping
- LINQ ordering by a child value
- Distinct() not working as expected, and not calling Equals method, on a List of user defined objects
- C# Linq String[] list minus from String[] list
- How to include any enum value in property with Linq where clause
- convert sql to linq with left outer join
- LinqDataSource - Can you limit the amount of records returned?
- SQL into LinQ Stement involving SUM()
- Make IQueryable via NHibernate prefer Join over Exists
- Linq with anonymous methods
- Find the most occurring number in a List<int>
- EF Core Inheritance Queries When Using a Single Table
- How much record can linq load when result stored in array?
- What's the most efficient way to compare these two lists?
- How to filter DataRow column containing url in c#
- How to extract data from Linq query
- Aggregate multiple properties of a class into a string