开发者

Filter a List of Objects Based on A Different List of Objects with Linq

开发者 https://www.devze.com 2023-03-12 21:45 出处:网络
I currently have two types of Lists: List (named LintMessages) and List (named FilterList). PCMessages Properties:

I currently have two types of Lists: List (named LintMessages) and List (named FilterList).

PCMessages Properties:
    public string filename { get; set; }
    public int linenumber { get; set; }
    public string messagetype { get; set; }
    public int messagecode { get; set; }
    public string description { get; set; }
    public string evaluated { get; set; }
    public string comment { get; set; }

Filter Properties:
    public bool applied { get; set; }
    public string messagetype { get; set; }
    public int messagecode { get; set; }
    public string evaluated { get; set; }
    public string filename { get; set; }

The goal is to exclude PCMessages from LintMessages that fit the criteria of any applied Filter and store them in List FiltLintMessages. For example, if FilterList contains two filters:

Filter1 Properties:
    applied = true;
    messagetype = Warning;
Filter2 Properties:
    applied = true;
    messagecode = 12;
    evaluated = "WIP";

Then I wish to create a List FiltLintMessages from LintMessages that excludes any entries where messagetype contains "Warning" or where messagecode=12 or where evaluated contains "WIP".

Code at time of writing:

FiltLintMessages = (from mess in LintMessages
                    from filt in FilterList
                    where !mess.evaluated.Contains(filt.evaluated)
                         && !mess.filename.Contains(filt.filename) 
                         && mess.messagecode != filt.messagecode
                         && !mess.messagetype.Contains(filt.messagetype)
                    select mess).ToList(); 

Any help would be appreciated.

/* Edited to work off of gaearon's suggested solution. Stepping through with debugger shows that this is the only relevant code to the problem. The correct items are in each list coming up to this block and on the assignment to FiltLintMessages, two separate filters 开发者_JS百科(exclude messagetype "Warning" and messagecode = 10, both applied) only excludes messagecode=10 PCMessages as it is the last applied filter in the list (tested with several instances of multiple filters). However, the code works with a single applied filter excluding both messagetype = "Warning" and messagecode = 10. Uploading any more of the code would not help and would bog down the post even more (the program is getting quite large). */

IEnumerable<PCMessage> messages = LintMessages;

foreach (LintMessageFilter filter in FilterList.Where(f => f.applied))
{
    messages = messages.Where(
               m => !m.evaluated.Contains(filter.evaluated)
                       && !m.filename.Contains(filter.filename)
                       && m.messagecode != filter.messagecode
                       && !m.messagetype.Contains(filter.messagetype)
                );
}

FiltLintMessages = messages.ToList(); //FiltLintMessages already initialized


This would apply each applied filter to the sequence in order.

IEnumerable<PCMessages> messages = LintMessages;

foreach (var filterIter in FilterList.Where(f => f.applied)) {
    var filter = filterIter; // capture current filter
    messages = messages.Where (m =>
                   !m.evaluated.Contains (filter.evaluated)
                   && !m.filename.Contains (filter.filename)
                   && m.messagecode != filter.messagecode
                   && !m.messagetype.Contains (filter.messagetype)
    );
}

var FiltLintMessages = messages.ToList ();
0

精彩评论

暂无评论...
验证码 换一张
取 消