score:14
var qry = headers.groupby(row => row.destinationlocation)
.todictionary(grp => grp.key, grp => grp.first());
or (equivalent):
var dictionary = (from row in headers
group row by row.destinationlocation)
.todictionary(grp => grp.key, grp => grp.first());
i wonder, though, if your current foreach
code isn't already better - it doesn't buffer the ones it intends to drop, for example.
score:2
var headerlocationlookup = populateheaders()
.aggregate(new dictionary<string, iheaderrecord>(), (header, d) => {
if(d.containskey(header.destinationlocation))
d[header.destinationlocation] = header;
return d;
});
i don't think this is any clearer than the existing code though.
score:5
i wrote a blog post a while back that shows you how you can create overloads of distinct that use a lambda expression as the key selector instead of a custom comparer, which would let you write:
headers.distinct(h => h.destinationlocation)
.todictionary(h => h.destinationlocation);
it does use a custom comparer underneath, but the extension method constructs that stuff for you, and makes it much easier to read.
Source: stackoverflow.com
Related Query
- Help Linqifying collection to Dictionary
- Group a collection and return a Dictionary
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- LINQ WHERE method alters source collection
- LINQ Source Code Available
- C# Code Contracts -- How to ensure that a collection of items contains items with unique properties?
- .NET 4 Code Contracts: "requires unproven: source != null"
- Finding 'Next Available' Key in a dictionary or related collection
- How to add TimeSpans together using LINQ when in a Dictionary collection
- C# Linq query help removing foreach loops creating cleaner code
- Linq expression. Help mimize code
- Separate a string into a list where is not in a collection Dictionary
- Convert object collection to dictionary using LINQ
- Is LINQifying my code worth accessing a foreach variable in a closure?
- creating Linq to sqlite dbml from DbLinq source code
- How to query MongoDb Collection with C# Driver by having dictionary values as filter parameters
- Collection of strings to dictionary
- Please help with a dictionary comprehension in LINQ, C#
- Better code for avoiding one dictionary - Case Sensitivity Issue
- How to Filter Nested Collection within Dictionary using LINQ C#
- How to get count of Dictionary Value Collection in C#
- How can I use LINQ to filter a collection of nested classes to yield a dictionary of unique properties of those classes?
- Convert a Model Class Collection to Dictionary based on property as KEY using LINQ C#
- EF Code first - add collection to a collection, appropriate usage of skip() and take()
- ViewModel with Collection of another ViewModel - Can AutoMapper Help Me Here?
- How to convert a Dictionary containing values as comma seperated strings into a Collection of KeyValuePairs?
- Converting Collection of Strings to Dictionary
- source code for LINQ 101 samples
- Update List of objects based on matching type in Dictionary and another List using C# collection
- Need help finishing or rewriting this algorithm for navigating a Generic Collection
More Query from same tag
- SingleOrDefault() when the sequence contains the default value
- Query in LINQ with self join
- IQueryable failback to IEnumerable
- Join 2 datatable on difference columns
- Run variable number of LINQ queries asynchronously
- Linq queries - not exists statement
- How to sum a field for all records and then return the max of all summed records with Linq
- How to select collection instead of collection of collections in SqlToEntity?
- convertion issue with DateTime being null in a LINQ query
- Single enumeration with multiple filters
- Update only a single item in linq
- Is it possible to Merge stacks with LINQ
- Linq Query - Finding consecutive increasing values
- How can I join a many to many relationship with linq
- Merge two datatables based on one primary column in C#
- Get all data from database for Administrator role but not for other roles, using same query
- Advanced filtering in linq
- Adding elements together across inherited arrays, LINQ
- Using LINQ to sort a Class that inherits from LIST<TTYPE>
- How to fix ViewState serialization error with my custom control?
- Compare two lists from two attributes and add differences to the third list C#
- Count duplicate rows in a datatable using vb.net
- How To show Grouped List on Razor View page?
- How to mock Linq Query with where predicate
- How to do sum of a column with LINQ?
- List<T> Any or Count?
- Using the Except clause for two DataTable objects
- Using linq to execute an expression
- Cannot convert lambda expression to type 'string' because it is not a delegate type - OrderBy and DbGeography by ref
- LINQ left join ordered with empty right-items at the end