C# 带有布尔方法的LINQ-Where子句

C# 带有布尔方法的LINQ-Where子句,c#,linq,boolean,where,C#,Linq,Boolean,Where,我有以下课程。我的目标是根据productTypeField和downloadSpeedField筛选组件。我有一个列表lstFilteredComps,其中我想添加下载速度小于或等于5的INTERNET productType组件,仅当存在大于5的组件时。如果所有组件都小于或等于downloadSpeed 5,则应添加downloadSpeed小于或等于5的INTERNET组件 课程: public ct_Component[] Components { get { return

我有以下课程。我的目标是根据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;
}
我尝试了下面的代码,但它删除了所有情况下downloadSpeed为5的组件,我将如何设置条件来检查downloadSpeed是否超过5。也就是说,我的代码应该仅在更高版本可用时删除组件,或者换句话说,只有当存在超过5MB的下载速度组件时,才应该调用
FilterInternetComponentLessThan5MB

ct_Component[] Components = response;
foreach (ct_Component comp in Components.Where(c => FilterInternetComponentLessThan5MB(c)))
{
    list<ct_Component> lstFilteredComps= //add filtered components;
}
我需要一张支票,比如:

foreach (ct_Component comp in Components.Where(Components.any(x=>x.productType == "INTERNET" && int.Parse(Regex.Replace(x.downloadSpeed, @"[^\d]", "")) > 5) ? true :  FilterInternetComponentLessThan5MB(c)))

您可能正在寻找
Any
方法,该方法接收一个私有参数,类似于
Where
,但它返回一个布尔值,就像集合中的一个项对谓词求值为true一样

你会得到这样的结果:

ct_Component[] components = response;
if (components.Any(c => c.DownloadSpeedNumber > 5))
{
    var newList = new List<ct_Component>();
    foreach (ct_Component comp in components.Where(FilterInternetComponentLessThan5MB))
    {
        //add filtered components to newList.
    }
ct\u组件[]组件=响应;
if(components.Any(c=>c.DownloadSpeedNumber>5))
{
var newList=新列表();
foreach(组件中的ct_组件组件组件,其中(过滤器内部组件小于5MB))
{
//将筛选的组件添加到新列表。
}
其中,
DownloadSpeedNumber
可以是安全解析现有
downloadSpeed
字段的getter或扩展方法


请记住,您将在
Any
方法中迭代数组,因此如果您有一个非常大的数组,您可能会重新考虑使用Linq,只需使用一个常规for,并只迭代一次。

给定两个谓词函数:

bool FilterNotInternetComponentOrOver5Mb(ct_Component component) =>
    (component.productTypeField != "INTERNET") ||
    (int.Parse(Regex.Replace(component.downloadSpeedField, @"[^\d]", "")) > 5);

bool FilterInternetComponentOver5Mb(ct_Component component) =>
    component.productTypeField == "INTERNET" &&
        int.Parse(Regex.Replace(component.downloadSpeedField, @"[^\d]", "")) > 5;
您可以使用以下方法基于5个以上的任意值进行筛选:

var hasOver5 = Components.Any(c => FilterInternetComponentOver5Mb(c));
var lstFilteredComps = Components.Where(c => !hasOver5 || FilterNotInternetComponentOrOver5Mb(c)).ToList();

FilterComponentLessThan5MB
如果速度大于5,则返回
true
,因此它确实是
speedgreetherthan5
组件。其中(speedgreetherthan5)
返回那些速度大于5的组件,而不是将其删除。是的。我的目标是在检查类似
的内容后调用
FilterComponentLessThan5MB
(ct_Component in Components.Where(Components.any(x=>x.productType==“INTERNET”和int.Parse(Regex.Replace(x.downloadSpeed,@“[^\d]”,“)>5)?true:c=>FilternetComponentLessThan5Mb(c))
基本上,如果我的条件为true,我想进行方法调用。因此类似于
if(downloadSpeed>5){foreach(ct_Component comp in components.Where(FilterInternetComponentLessThan5MB)}其他{foreach(ct_Component comp in components)}
我相信
任何
都会帮助您实现这一点
var hasOver5 = Components.Any(c => FilterInternetComponentOver5Mb(c));
var lstFilteredComps = Components.Where(c => !hasOver5 || FilterNotInternetComponentOrOver5Mb(c)).ToList();