Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表布尔函数的LINQ帮助_C#_Linq_Lambda_Expression - Fatal编程技术网

C# 列表布尔函数的LINQ帮助

C# 列表布尔函数的LINQ帮助,c#,linq,lambda,expression,C#,Linq,Lambda,Expression,如何构造LINQ表达式以从一个列表中删除满足返回布尔值的函数条件的值 string[] message = "days of the week" message.ToList().RemoveAll(c=>checkShortWord(c)); public static bool checkShortWord(string word) { if ((word.Length > 3) &&

如何构造LINQ表达式以从一个列表中删除满足返回布尔值的函数条件的值

string[] message = "days of the week"
message.ToList().RemoveAll(c=>checkShortWord(c));

public static bool checkShortWord(string word) {
       if ((word.Length > 3) &&                        
          (!Regex.IsMatch(word, "^[0-9]+$")))          
        return true;

      return false;
}
我的结束字符串数组现在应该是:

message = {"days","week"}

我应该换什么?我的消息数组永远不会改变。

三件事。第一,消息不是数组(我假设它在您的真实代码中)。第二,你的方法是反向的。第三,你没有保留对列表的引用

var list = message.ToList();
list.RemoveAll(word=>word.Length <= 3 || Regex.IsMatch(word, "^[0-9]+$"));

三件事。第一,消息不是数组(我假设它在您的真实代码中)。第二,你的方法是反向的。第三,你没有保留对列表的引用

var list = message.ToList();
list.RemoveAll(word=>word.Length <= 3 || Regex.IsMatch(word, "^[0-9]+$"));

您正在构建一个新列表,并从该列表中删除项目,然后将其丢弃。如果需要缺少已删除项的数组,则需要创建一个新数组:

string[] message = "days of the week".Split(' ');
message = message.Where(c => checkShortWord(c)).ToArray();
或者,您可以使用
列表
而不是
字符串[]
,然后使用RemoveAll方法就地修改它:

List<string> message = "days of the week".Split(' ').ToList();
message.RemoveAll(c => !checkShortWord(c));

您正在构建一个新列表,并从该列表中删除项目,然后将其丢弃。如果需要缺少已删除项的数组,则需要创建一个新数组:

string[] message = "days of the week".Split(' ');
message = message.Where(c => checkShortWord(c)).ToArray();
或者,您可以使用
列表
而不是
字符串[]
,然后使用RemoveAll方法就地修改它:

List<string> message = "days of the week".Split(' ').ToList();
message.RemoveAll(c => !checkShortWord(c));

不要给你的方法命名
checkShortWord
。那太令人困惑了。以它真正检查的内容命名,例如
IsShortWord
。然后,lambda表达式如下所示:

message.ToList().RemoveAll(c => IsShortWord(c));
换句话说,删除列表中所有短单词的成员。当然,如果要对结果执行任何操作,还需要将结果指定给变量


另外,在当前函数中,true和false看起来是向后的。

不要命名方法
checkShortWord
。那太令人困惑了。以它真正检查的内容命名,例如
IsShortWord
。然后,lambda表达式如下所示:

message.ToList().RemoveAll(c => IsShortWord(c));
换句话说,删除列表中所有短单词的成员。当然,如果要对结果执行任何操作,还需要将结果指定给变量


另外,在当前函数中,true和false看起来是向后的。

假设您实际上有一个列表(
IEnumerable
),而不是错误的
消息
变量,并且
checkShortWord
实际返回
true
用于短词,那么您应该执行以下操作:

IEnumerable<string> before = new [] {"days", "of", "the", "week"};
IEnumerable<string> after = before.Where(word => !checkShortWord(word)); 
IEnumerable before=new[]{“days”、“of”、“the”、“week”};
IEnumerable after=before.Where(word=>!checkShortWord(word));

假设您实际上有一个列表(
IEnumerable
),而不是错误的
消息
变量,并且
checkShortWord
实际返回
true
,那么您应该执行以下操作:

IEnumerable<string> before = new [] {"days", "of", "the", "week"};
IEnumerable<string> after = before.Where(word => !checkShortWord(word)); 
IEnumerable before=new[]{“days”、“of”、“the”、“week”};
IEnumerable after=before.Where(word=>!checkShortWord(word));

Ah。。可以我试试这个。谢谢啊。。可以我试试这个。谢谢