我目前有两种类型的列表:List(名为LintMessages)和List(名为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; }
目标是从符合任何应用过滤器条件的LintMessages中排除PCMessages,并将它们存储在List FiltLintMessages中。例如,如果FilterList包含两个过滤器:
Filter1 Properties:
applied = true;
messagetype = Warning;
Filter2 Properties:
applied = true;
messagecode = 12;
evaluated = "WIP";
然后我希望从LintMessages创建一个List FiltLintMessages,它排除messagetype包含“Warning”或messagecode = 12或者evaluate包含“WIP”的任何条目。
撰写时的代码:
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();
任何帮助都将不胜感激。
/ *编辑处理gaearon建议的解决方案。单步执行调试器会显示这是该问题的唯一相关代码。每个列表中的正确项目都在此块中,并且在分配给FiltLintMessages时,两个单独的过滤器(排除消息类型“警告”和消息代码= 10,两者都应用)仅排除messagecode = 10 PCMessages,因为它是最后应用的过滤器列表(使用多个过滤器的多个实例进行测试)。但是,代码适用于单应用的过滤器,不包括messagetype =“Warning”和messagecode = 10.上传任何更多的代码都无济于事,甚至会使帖子陷入困境(程序是变得非常大)。 * /
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
答案 0 :(得分:1)
这将按顺序将每个applied
过滤器应用于序列。
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 ();