score:2
Accepted answer
you can accomplish this using linq's any
method:
var query = toienumerable.groupby(c => c.country).select(r =>
{
bool somewithnogrades = r.any(i => i.grade == null);
return new
{
r.key,
sumgrade = !somewithnogrades ? r.sum(f => f.grade) : null
};
});
Source: stackoverflow.com
Related Query
- How to return null in a GroupBy statement when one of the values in the non-key column contains null?
- How to make a linq Sum return null if the summed values are all null
- How to return null when the Linq query returns empty value?
- How to GroupBy using LINQ query in C# when the key is to be the same type as your type T when grouping List<T>
- How to run linq query with select when a primary key column returns null and thus the related attribute is null
- How to select one item in LINQ when the other is null
- How to return all values of a column as null except one column value in LINQ?
- How to LINQ Join when one key is an ArrayOfInt and the other an int
- How to insert a record with LINQ and C# and return the Primary Key of that record
- How to get linq `ForEach` statement to return data on the method call being made for each list object?
- How do I use Linq ToDictionary to return a dictionary with multiple values in the dictionary items?
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Linq How to Get Average When all values equals null or 0 ? MVC
- When using a LINQ Where clause on a Dictionary, how can I return a dictionary of the same type?
- How do I combine the keys and values of a Dictionary into one List using LINQ?
- Why do I get a "key already added" error when using the key from a GroupBy in ToDictionary?
- How can I make my Linq select return values if the value selected is null?
- How can I split a List<T> into two lists, one containing all duplicate values and the other containing the remainder?
- Linq Groupby categorizes but how do I manipulate the values in the collection
- How to get the key when using linq Max() method
- How do I do a LINQ GroupBy where the key can be reversed?
- C# - Select out the lowest of two values when duplicate key is found
- Linq GroupBy - how to specify the grouping key at runtime?
- why null values not return in when apply != in linq
- This LINQ statement crashes if one of the Properties is NULL. How can I fix this?
- How Return all when Using Take in Linq statement in C#
- How to find the key of a dictionary when the value is a list of objects in C#?
- How do I compare two lists where one list starts with the other, and only return the "overhang" elements?
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to use CollectionAssert (and Linq?) to find all the values for one property, are the same?
More Query from same tag
- Linq to CSV select by column
- How to add a row number column in linq
- How to join two table from two different edmx using linq query
- LINQ returns wrong number of items when DataLoadOptions is set
- Approach - Optimizing LINQ for performance
- Find all possible sequence list from single list c#
- Linq nested collection filter
- dynamically change LINQ to Entity query
- join diffrent field type in linq
- Unable to step into or break in method called inside Linq query/expression
- SQL To Get Parent From Self Referencing Table
- Hierarchy Employee/Manager from a List
- Dictionary<K, List<TObject>> from collection of objects
- Breaking a loop using TakeWhile when a count or length condition is meet
- 'System.Linq.IQueryable' does not contain a definition for 'OrderByDescending'
- How do you manually construct a lambda expression?
- How can I run this function inside LINQ query
- Check if chars of a string contains in another string with LINQ
- Parse XML with XDocument C#
- LINQ - Comparing a search term with multiple entries against another string with multiple entries
- Can't get result of desired query in linq
- Using LINQ in an MVC project, how can I orderby a joined table's value?
- LINQ Self Referencing Table SELECT Query
- Sorting a Dictionary<string,Object>, with multiple levels
- Can I split an IEnumerable into two by a boolean criteria without two queries?
- How to scan XElement and put all the element ( with value ) in Dictionary?
- LINQ OrderBy different field type depending on IF statement
- Filter generic list with generic dictionary
- Problem with nullable decimal with Entity Framework
- LINQ - can it backtrack?