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: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.
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);
Source: stackoverflow.com
Related Query
- Calculating the sum of the values in a dictionary that exist in a generic list
- The fastest way to find dictionary keys that are not exist in another list
- How to calculate the sum of all values in a dictionary excluding the first item's value?
- Convert a list to a dictionary and sum up values using linq
- 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?
- How can I get the average of the elements of a generic list that meet a criteria?
- Get items from input list that do not exist in the database using EF Core 2.1
- How can I sum values across a generic list using LINQ?
- Linq for selecting elements from list 1 that exist on list 2 by comparsion between 2 properties values
- How to count the occurence of each number in list A that is exist in list B and return zero if not existed?
- Verify that a list has all the other list values
- Getting the sum of values in a nested dictionary
- Select all from one List, replace values that exist on another List
- I need to find max total of the sum of integers in Generic list using Linq
- using Linq to sort a list based on the sum of values in each item
- Linq - Get all keys from dictionary where all values in the value list are equal to criteria
- Compare keys of dictionary with values of List and return all the matching values including duplicates
- Get keys (objects) from a dictionary that contains list and filtered by values
- Getting Sum of Multiple Columns and summing up values for the same id from a list
- merge several lists but keep only values that are found on all the list
- Check a list of DTOs using linq to see if all values that exclude a particular value all equal the same thing
- How can I retrieve an object from a generic list based on the value of two of that object's members?
- Get The Full List Of Objects In A Dictionary Values
- Using LINQ how do you filter a list of strings for values that match the pattern "q1, q2" etc.?
- C# Dictionary filtering (LINQ) and list extension method filtering values and getting the key
- linq: how to divide a list into two by the sum of the values and keeping the input order
- How can I flatten a dictionary of dictionaries and sum the values of the inner dictionary with LINQ?
- When I create a list that holds the "id" values of other database tables, I cannot reach the "name" values for the related tables
- c#- select keys and values that does not exist in another dictionary
More Query from same tag
- C# lambda extract a single row string value
- I'm having trouble with auto complete
- group list a join with list b and get the average of items in list b
- Select returning only one element
- Get Neighboring Entities
- how to group by multiple columns using linq
- excluding templates from search in sitecore
- Order by Descending with multiple comparisons
- MemberExpression: InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined
- LINQ Query Issues when trying to select from empty collection
- How to return list of items from linq to sql query and call it in a console application
- Issues with DateTime.AddMinutes() in Dynamic LINQ Query
- Which Assembly contains LINQ extension method Delete?
- LINQ Updating Sort Order to nearest possible value
- n th day from date month
- LINQ Query to remove duplicate items in a List based on the value
- Concat items in linq
- Please help with a dictionary comprehension in LINQ, C#
- Linq performance poor
- Removing an object from IOrderedEnumerable
- Is there a Way to use Linq to Oracle
- Retriving Sum of values under object collection using linq c#
- How to map a default LINQ expression in a generic class
- how to get DatakeyValue from gridview being populated with linq
- Flatten a list in linq
- Cannot initialize type '' with a collection initializer because it does not implement 'System.Collections.IEnumerable'
- Update multiple fields using single linq statement
- c# filtering list of list of integers if empty
- how to Update the XMl value and write back using LINQ
- Linq Reflection Query to Lookup an Object's Property Name by Attribute