Linq Func delegate can it be added to?

I have a method below.

If possible I would like to add some standard filtering in addition that passed through the func variable:

.Where(p => p.IsLive)

I can of course pass it in the function string itself, however, I would need to remember to do so each time. I've tried various ways of incorporating it with the func variable but haven't managed to crack the syntax (if it is possible). Any advice?

Product GetProduct(Func<Product, bool> func)
{
    return CompareView.Select()
        .Where(func) // Need to add std filtering here
        .FirstOrDefault();
}
Jon Skeet
people
quotationmark

The simplest approach would just to be to chain another call to Where:

Product GetProduct(Func<Product, bool> func)
{
    return CompareView.Select()
        .Where(p => p.IsLive)
        .Where(func)
        .FirstOrDefault();
}

You could create a new delegate like this:

Product GetProduct(Func<Product, bool> func)
{
    return CompareView.Select()
        .Where(p => p.IsLive && func(p))
        .FirstOrDefault();
}

... but personally I don't think that's as simple as the where-composition approach.

people

See more on this question at Stackoverflow