【发布时间】:2019-06-26 07:02:18
【问题描述】:
我有以下课程。我的目标是根据 productTypeField 和 downloadSpeedField 过滤组件。我有一个列表lstFilteredComps,我想在其中添加下载速度小于或等于 5 的 INTERNET productType 组件,前提是存在大于 5 的组件。如果所有组件都小于或等于 downloadSpeed 5,则应添加 downloadSpeed 小于等于 5 的 INTERNET 组件。
类:
public ct_Component[] Components {
get {
return this.componentsField;
}
set {
this.componentsField = value;
}
}
public partial class ct_Component
{
private string descriptionField;
private string downloadSpeedField;
private string productTypeField;
}
我尝试了以下代码,但它在所有情况下都会删除下载速度为 5 的组件,我将如何设置条件来检查是否存在大于 5 的下载速度。也就是说,我的代码只有在更高可用时才应该删除组件,或者换句话说,只有在存在超过 5MB 的 downloadSpeed 组件时才应该调用 FilterInternetComponentLessThan5MB。
ct_Component[] Components = response;
foreach (ct_Component comp in Components.Where(c => FilterInternetComponentLessThan5MB(c)))
{
list<ct_Component> lstFilteredComps= //add filtered components;
}
FilterComponentLessThan5MB 方法
private bool FilterComponentLessThan5MB(ct_Component component)
{
if (component.productType != "INTERNET" || (component.productType == "INTERNET"
&& int.Parse(Regex.Replace(component.downloadSpeed, @"[^\d]", "")) > 5))
{
return true;
}
else
return false;
}
我需要一个类似的检查:
foreach (ct_Component comp in Components.Where(Components.any(x=>x.productType == "INTERNET" && int.Parse(Regex.Replace(x.downloadSpeed, @"[^\d]", "")) > 5) ? true : FilterInternetComponentLessThan5MB(c)))
【问题讨论】:
-
FilterComponentLessThan5MB如果速度 > 5 则返回true,所以它确实是speedGreaterThan5和Components.where(speedGreaterThan5)返回速度 > 5 的那些而不是删除它。 -
是的。我的目标是在检查
foreach (ct_Component comp in Components.Where(Components.any(x=>x.productType == "INTERNET" && int.Parse(Regex.Replace(x.downloadSpeed, @"[^\d]", "")) > 5) ? true : c => FilterInternetComponentLessThan5MB(c)))之类的内容后致电FilterComponentLessThan5MB