score:1
Accepted answer
How about this (you can use .Select
instead of .SelectMany
to get separate groups for each product. .SelectMany
combines all the valid result records into a single list):
Products
.GroupBy(p => p.Name)
.SelectMany (grp =>
grp.OrderBy(p => p.Priority) // sort by priority
.SkipWhile(p => p.Value == null) // skip null entries at beginning
.Reverse() // reverse
.SkipWhile(p => p.Value == null) // skip null entries at end
.Reverse() // reverse back to normal
.Where(p => p.Value == null) // then find null entries
);
Demo: http://ideone.com/2dU9L
score:2
It would be easier and more efficient if you determined which values are your start and stop points in the group and filter from there.
var query =
from product in Products
group product by product.Name into g
let ordered = g.OrderBy(p => p.Priority).ToList()
let firstIndex = ordered.FindIndex(p => p.Value != null)
let lastIndex = ordered.FindLastIndex(p => p.Value != null)
select new
{
Product = g.Key,
Values = ordered
.Skip(firstIndex + 1)
.Take(lastIndex - firstIndex - 1)
.Where(p => p.Value == null),
};
Source: stackoverflow.com
Related Query
- Using Linq to check if a list includes null values in the middle
- Using Max in LINQ query - null values in the list
- Check a list of DTOs using linq to see if all values that exclude a particular value all equal the same thing
- Find the Second Max in a list of values using linq c#
- Check null value in a list using linq
- Check if List is not null when using "contains" in LINQ query else select all records
- How to check if two values exist in a list using linq in c#
- Count the number of Enum values in list using Linq
- Get the set of grouped values as a list using linq
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- How to check comma separated values stored in data base column having the given value using linq in c#
- How to retrieve the values from list using indexes by linq query?
- c# linq how to check for null values in the same column that i need to be not null
- using Linq to sort a list based on the sum of values in each item
- Check if list is null before using linq
- Using LINQ to check for anything in a list that is not null or blank
- Using LINQ how do you filter a list of strings for values that match the pattern "q1, q2" etc.?
- C# Linq Comparing List Values using multiple fields and return item which doesn't satisfy the condition
- Comparing a list of values against the property of an object (list) using LINQ
- check if a list contains all the element in an array using linq
- Remove duplicates in the list using linq
- How to select values within a provided index range from a List using LINQ
- Get indexes of all matching values from list using Linq
- Convert dictionary values to list using linq
- Select distinct values from a list using LINQ in C#
- How to get a list of the grouped values in linq groupby?
- How to make a linq Sum return null if the summed values are all null
- Check that all items of IEnumerable<T?> has the same value using LINQ
- Change the property of objects in a List using LINQ
- in C#, how do I order items in a list where the "largest" values are in the middle of the list
More Query from same tag
- Can I use FirstOrDefault() or First() inside Where()
- Filter LINQ query using items from an external list with Lambda
- linq how to select a parent with a child collection that contains one or many of an array (or list) of values
- Entity Framework - Efficient eager loading with long chains of dependent objects?
- How do I select a person only if their Id appears in another table?
- How to get top 4 result from a list using linq c#
- Dynamic LINQ API with .NET Framework 4
- Simple Linq to SQLIte application hangs on SubmitChanges()
- how do i build dynamic linq queries at runtime using strings?
- LINQ Include error
- How insert multiple value in Intermediate table through API
- More C# to VB conversion woes when working with Mediator helpers in wpf
- Find all keywords
- How to resolve collation conflicts with the Entity Framework?
- Linq to XML Simple Query
- How do I use LINQ to .Select from a List inside a Map?
- Displaying content under an ID
- Comparing Enumerable with enumerable in a linq query
- PredicateBuilder stripping brackets, breaking nested AND's combined with OR
- Comparing fields
- Is it possible to have a make a generic method which can take the place of three methods with a System.Data.Linq.Mapping.FunctionAttribute?
- Taking distinct item from list of datapoints
- How to get all records from thisa year onwards from LINQ to SQL model?
- Selecting as expression from a single object
- Retrieving data from WPF Datagrid into a List does not have the same number of columns
- How To Determine Which WHERE Clause Was True In A Linq Statement
- How to add seconds and hours in Stored procedure sql
- LINQ GroupBy Select only where key is not null
- Merging arrays of different sizes with user-implemented zip function
- Use a foreach loop within a foreach loop in a linq query