I have a requirement where I need to filter out the null values from a list, only if the null value appears after the first non-null value and before the last non-null value.
Product one = new Product { Name="A" Priority="1" Value=null };
Product two = new Product { Name="A" Priority="2" Value=null };
Product three = new Product { Name="A" Priority="3" Value="10" };
Product four = new Product { Name="A" Priority="4" Value=null };
Product five = new Product { Name="A" Priority="5" Value="20" };
Product six = new Product { Name="A" Priority="6" Value=null };
In the example, I need to first sort the list of products based on their priority and then check the first non-null value (ie., priority 3) and last non-null value (ie, priority 5), then get the list of all products with null values within priority 3 & 5. So, in our example only Product 4 with Priority 4 is the record I am looking for.
I got to the part of actually grouping them by products and sorting them by priority but stuck on how to proceed after that
from p in Products
group p by p.Product into grp
select new
{
Product = grp.Key
Values = grp.OrderBy(x => x.Priority)
}
Can someone point me as how to proceed? I am thinking I may to use the indexes to identify all the non-null and iterate through to get min and max Priority values and later query for all records with blank null values within the min/max priority.
How about this (you can use
.Selectinstead of.SelectManyto get separate groups for each product..SelectManycombines all the valid result records into a single list):Demo: http://ideone.com/2dU9L