score:11
Just so:
var sum = selectedList.Sum(s=>cars[s]);
Or if you are not sure, that all values are in the dictionary (thanks Tim Schmelter for correction):
var sum = selectedList.Sum(s=>cars.ContainsKey(s)?cars[s]:0);
EDIT: Even better is from Tim proposed solution:
selectedList.Where(cars.ContainsKey).Sum(s => cars[s])
score:5
You could filter the items from the dictionary and then sum the values:
var sum = cars
.Where(item => selectedList.Contains(item.Key))
.Sum(item => item.Value);
score:0
Alternative non LINQ answer:
decimal total = 0;
foreach (var car in selectedList)
{
total += cars[car];
}
score:1
So here's a different approach that might perform a bit better for some edge cases:
var sum = selectedList
.Distinct()
.Sum(s =>
{
decimal d;
if (cars.TryGetValue(s, out d))
{
return d;
}
return 0;
}
);
You only want distinct values from the list iterated, and this way you'll avoid duplicating lookups in the cars
dictionary (if that's not the case, then just comment out the Distinct()
). This should offer a good balance of performance regardless of whether the list or the dictionary are larger.
Source: stackoverflow.com
Related Articles
- Calculating the sum of the values in a dictionary that exist in a generic list
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- Select all from one List, replace values that exist on another List
- Get keys (objects) from a dictionary that contains list and filtered by values
- The fastest way to find dictionary keys that are not exist in another list
- c#- select keys and values that does not exist in another dictionary
- Convert dictionary values to list using linq
- Linq selecting items that exist in both list
- Exclude list items that contain values from another list
- LINQ: Getting Keys for a given list of Values from Dictionary and vice versa
- LINQ Lambda - Find all ID's in one list that don't exist in another list
- Convert a list to a dictionary and sum up values using linq
- Search dictionary values and return List of keys meeting conditions
- How to select values in list that are NOT IN a Table using EF Core?
- Verify if a list (or a sublist of that list) of decimal values can equal a certain sum
- How do I combine the keys and values of a Dictionary into one List using LINQ?
- Select All object values to list from dictionary
- Joining Dictionary key that matches class List's list item into a new list - C#
- How to convert Dictionary to List with mixing of values and keys?
- How can I get the average of the elements of a generic list that meet a criteria?
- List of objects to Dictionary with distinct keys and selected values
- Linq: Is there a way to search a list of list of objects for values that match a condition?
- Using LINQ I have a list of lists, how do I select all objects that exist in every list?
- How to create a predicate that must meet two values with one being a list of values?
- Get items from input list that do not exist in the database using EF Core 2.1
- Excluding items from a list that exist in another list
- How can I sum values across a generic list using LINQ?
- How to check for null values before Converting a comma Separated string to Generic List
- How to convert dictionary with object to list of that type?
- How to check if two values exist in a list using linq in c#
- LINQ SingleOrDefault, Brush, what I am doing wrong?
- How to Check Whether an Array (or List) Contains Another an Array (List) Using LINQ
- return multiple values in static class
- Linq MVC5 MSQL select some columns from a table containing larg amount of columns in order to make better and fast query
- Replace {#Text} and {$Text} in a string, in a performant way
- How to neatly query corresponding object array items?
- Which methods to close a compiled query
- Sorting not working for the year "2013"
- Linq: Filter a list with a different IEnumerable<bool>
- Displaying random rows without repetition
- How to query SQLite for whether a record exists?
- Multi-delimited string to list of objects using linq and lambdas
- How to translate a query with an EntityState and a value from Entity Framework 5 to 6?
- Filtering datatable where sum of grouped column value is not zero
- How can I select the minimum sub-sequence using LINQ?
- Entity Framework 4 - What is the syntax for joining 2 tables then paging them?
- Filtering rows of a 2D String matrix by string value
- Dynamically build a LINQ statement
- LINQ Query with grouping and ranking
- IQueryable to JObject