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),
    };

Related Articles