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 Query
- What would be the Linq code for this grouping using VB.NET 2008?
- How to reuse a linq expression for 'Where' when using multiple source tables
- Using Linq for grouping
- Using LINQ query result for data source for GridControl c#
- Grouping earliest entry for each day using Linq
- Code Rewite for tuple and if else statements by using LINQ
- source code for LINQ 101 samples
- LINQ Intersect Query for Many to Many relationship using .NET4 MVC4 Code First
- Grouping flat data to create a hierarchical tree using LINQ for JSON
- how to fetch data from database using linq query for relationship 1:N and N:N (between 3 entity) in asp.net mvc EF code first?
- Convert string[] to int[] in one line of code using LINQ
- Using LINQ for an object that only has GetEnumerator()
- Extension method for IQueryable left outer join using LINQ
- Performance for using 2 where clauses in LINQ
- Key comparisons for Linq GroupBy using Default EqualityComparer
- Steps for a beginner to run very basic linq to sql query using Linqpad
- Cast Entity to Implemented Interface in a Generic Method Using LINQ for Entity Framework
- Using LINQ to search a byte array for all subarrays that start/stop with certain byte
- Workarounds for using custom methods/extension methods in LINQ to Entities
- Need help in a grouping using LINQ in C#
- Any better way for converting array to concurrent dictionary using linq or IEnumerable?
- Dynamic linq query expression tree for sql IN clause using Entity framework
- Grouping by hour using LINQ
- Suggestions for designing complex LINQ code
- Building a dynamic where clause for dynamic keywords or using IQueryable C# Linq
- Left outer join using LINQ -- understanding the code
- Is there a suggested pattern for using LINQ between the Model & DataAccess Layers in a DDD based Layered Architecture
- Using LINQ how do I have a grouping by a "calculated field"
- Avoiding code repetition when using LINQ
- Best way to check if value exists for a key in ILookup<string, string> using linq
More Query from same tag
- Using c# to fill a variable with a LINQ any match
- linq left outer join on lists
- Create a C# Generic class for repeated code
- Asp.net mvc Linq query for multiple filters
- Linq to List: Is there a way to include all the columns after applying a groupby on list
- Linq to SQL nullable int?
- LINQ: Select on dynamic list doesn't compile
- LINQ to Entiites, left outer join of a group by
- Linq equivalent to ColumnName = @Param or @Param IS NULL
- dynamic sort columns doesn't work in linq
- linqpad hide specific column or restrict row to single line
- How to make LINQ to EF query more efficient
- Why can not get query value in a label?
- Using Linq select an item outside an List
- Construct a new Dictionary based on value criteria
- LINQ Query find columns where string contains any letter
- LINQ GroupBy for multiple Column
- Get data from three tables by ID C# APi
- Jointure in linq with a regular expression
- How do I use Entity Framework to find a record in Cosmos with case insensitive conditions
- LINQ select items from set A which are not in set B
- Linq Union/UnionAll/Concat
- How to Hide Table in MVC View when there is No Data?
- How to convert nulls to specific values dependent on type efficiently?
- linq group by with join
- Collection Initializer adding items in wrong order within linq select
- How to add SelectMany to an Expression Tree with Expression.Call
- Create an expression tree in c#
- Build a dictionary of objects and append text to one of its properties using Linq
- Object reference not set to an instance of an object error attempting to find an object recursively using LINQ