views:

40

answers:

3

Hi,

I am attempting to filter a list based on a predicate Func. I use Func to filter the complete list and return the result of the operation. I then need to determine which MachineActions appear in the complete list but not the filtered list.

static void Main(string[] args)
{
    var allActions = new List<MachineAction>
                     {
                         new MachineAction
                             { Name = "A", Status = MachineActionStatus.Cancelled },
                         new MachineAction
                             { Name = "B", Status = MachineActionStatus.Error },
                         new MachineAction
                             { Name = "C", Status = MachineActionStatus.Waiting }
                         };

     var filteredActions = allActions.Where(FilterNotCompleted);
}

private readonly static Func<MachineAction, bool> FilterNotCompleted = action =>
                                         (action.Status == MachineActionStatus.Waiting ||
                                          action.Status == MachineActionStatus.Started);

public class MachineAction
{
    public MachineActionStatus Status { get; internal set; }
    public string Name { get; internal set; }
}

internal enum MachineActionStatus
{
    Waiting,
    Started,
    Success,
    Error,
    Cancelled
} ;

Is there an extension method that will compare the full list to the filtered list and return those not in both?

Thanks

+2  A: 

Take a look at the Except extension

Richard Friend
A: 
allActions.Where(item => !filteredActions.Contains(item))

but maybe better would be to do

var filteredActions = allActions.Where(item => !FilterNotCompleted(item));

(note the ! bevore the func command.)

cRichter
A: 
allActions.Except(filteredActions )
Space Cracker