score:7
Accepted answer
That's because CategoryId
is a nullable. So you need to select it's Value
property first:
products.ToList()
.Where(p => p.CategoryId.HasValue)
.Select(p => p.CategoryId.Value)
.GroupBy(i => i)
.ToDictionary(g => g.Key, g => g.Count());
score:4
How about this?
.ToDictionary(pgroup => pgroup.Key ?? -1, pgroup => pgroup.Count());
And regarding your syntax error with the anonymous type, the correct syntax is as follows:
.Select(p => new { Key = p.Key ?? -1, Count = p.Count() })
score:5
Simply use
products.ToList()
.GroupBy(p => p.CategoryId)
.Where(pgroup => pgroup.Key.HasValue)
.ToDictionary(pgroup => pgroup.Key.Value, pgroup => pgroup.Count());
score:0
You need to filter out the null values, and then use the .Value
property of the int?
as the grouping key:
products.ToList()
.Where(p => p.CategoryId.HasValue)
.GroupBy(p => p.CategoryId.Value)
.ToDictionary(pgroup => pgroup.key, pgroup => pgroup.Count());
Source: stackoverflow.com
Related Articles
- Convert Dictionary<int, int?> to Dictionary<int, int> by skipping null values with LINQ
- EF Code First comparing null values generates strange query
- convert object list to type array and remove null values
- Convert string to decimal within LINQ Average function while handling possible null values
- Convert string[] to int[] in one line of code using LINQ
- Convert SQL to Linq left join with null
- Filtering Null values in Select
- In LINQ, select all values of property X where X != null
- Convert dictionary values to list using linq
- Checking a list with null values for duplicates in C#
- C# Linq OrderBy filtering null or empty values to be last
- LINQ Order By Descending with Null Values on Bottom
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- Sorting a list with null values
- Using Linq's Where/Select to filter out null and convert the type to non-nullable cannot be made into an extension method
- How to make a linq Sum return null if the summed values are all null
- Convert a list to a dictionary and sum up values using linq
- Linq How to Get Average When all values equals null or 0 ? MVC
- C# Lambda returns some null values
- How to handle null values in linq?
- Entity Framework - Only update values that are not null
- Linq to Dictionary<string, List<string>> with Null values
- LINQ Source Code Available
- LINQ LEFT JOIN not working on NULL values
- C# LINQ: remove null values from an array and return as not nullable
- LINQ to SQL and null values
- Linq to DataSet - Handling Null Values
- Difference between the returned values of two selector functions in this code
- Queryable.Aggregate is not working with null values
- .NET 4 Code Contracts: "requires unproven: source != null"
- How to SUM the value from DISTINCT row using LINQ
- Using LINQ to iterate combinations
- The model item passed into the dictionary is of type .... {Models.App} but what I passed is {Models.App, System.Double}
- Join sub query table to table function with outer apply using Entity Framework
- LINQ InheritanceMappingAttribute Code property
- Contains on list is too slow, how to improve?
- Get 3 random values from a Dictionary (with tuple string[]) without duplicates
- Design advice. Using DataTable or List<MyObject> for a generic rule checker
- LINQ - Sequence contains no elements
- How to find max value for each column using LINQ (vb.net)
- Proper Code First EF6 structure for related classes/tables
- using case statement in a join with linq
- How to sort an alphanumeric listbox?
- How to combine results from multiple linq queries and then sort?
- grouping: for comprehensions in scala vs .net linq
- How do I query for when a field doesn't begin with a letter?
- System.Linq.Dynamic.DynamicExpression parsing expressions with methods
- How to match two Lists with only items that are different in Linq
- Get the Count and Name
- LINQ - Add static text to each result