C# 使用谓词计算单词数
我需要获取满足条件的字符串[]中指定的项目计数。所以,我尝试了谓词,并使用相同的谓词定义了我的条件。但是我的代码不起作用。有人能帮我吗C# 使用谓词计算单词数,c#,predicate,C#,Predicate,我需要获取满足条件的字符串[]中指定的项目计数。所以,我尝试了谓词,并使用相同的谓词定义了我的条件。但是我的代码不起作用。有人能帮我吗 string[] books = new string[] { "Java", "SQL", "OOPS Concepts", "DotNet Basics"}; Predicate<string> longBooks = delegate(string book) { return book.Length > 5; }; int numbe
string[] books = new string[] { "Java", "SQL", "OOPS Concepts", "DotNet Basics"};
Predicate<string> longBooks = delegate(string book) { return book.Length > 5; };
int numberOfBooksWithLongNames = books.Count(longBooks);
string[]books=newstring[]{“Java”、“SQL”、“OOPS概念”、“DotNet基础”};
谓词longBooks=delegate(stringbook){returnbook.Length>5;};
int numberOfBooksWithLongNames=books.Count(longBooks);
当我运行它时,它显示一个编译时错误。请参阅下文:
“string[]”不包含“Count”的定义,并且最佳扩展方法重载“System.Linq.Enumerable.Count(System.Collections.Generic.IEnumerable,System.Func)”具有一些无效参数
试试这个:
var result = books.Count(x => x.Length > 5);
在没有lambdas匿名方法的情况下执行此操作时,请定义一个方法(您的谓词):
用户可以使用它:
var result = books.Count(HasLongTitle);
你可以试试看
books.Count(a = > a.Length > 5);
LINQ方法不将谓词
作为参数。在您的例子中,该方法接受类型为Func
的委托。因此,有几种方法可以修复代码,最简单的可能是按照其他人的建议使用lambda。或者,使用原始代码只需将谓词
更改为Func
:
string[]books=newstring[]{“Java”、“SQL”、“OOPS概念”、“DotNet基础”};
Func longBooks=委托(字符串书){return book.Length>5;};
int numberOfBooksWithLongNames=books.Count(longBooks);
有两个问题
string[]' does not contain a definition for 'Count' and the best extension method
overload 'System.Linq.Enumerable.Count<TSource>
(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)'
has some invalid arguments
string[]”不包含“Count”和最佳扩展方法的定义
重载'System.Linq.Enumerable.Count
(System.Collections.Generic.IEnumerable,System.Func)'
有一些无效的参数
及
参数2:无法从“System.Predicate”转换为
'System.Func'
这些解决方案有效
int numberOfBooksWithLongNames = books.AsEnumberable().Count(s => longBooks(s));
int numberOfBooksWithLongNames = new List<string>(books).Count(s => longBooks(s));
int numberOfBooksWithLongNames = books.Count(s => longBooks(s));
int numberOfBooksWithLongNames=books.AsEnumberable().Count(s=>longBooks);
int numberOfBooksWithLongNames=新列表(书籍).Count(s=>longBooks);
int numberOfBooksWithLongNames=books.Count(s=>longBooks);
请添加有关其不起作用原因的详细信息。我已编辑了您的标题。请参见“”,其中一致意见是“不,他们不应该”。与其使用这样的anon委托,您可以使用lambdabooks.Count(book=>book.Length>5)
读起来更好。您能告诉我如何使用谓词进行此操作吗。这实际上是一个谓词,但是我编辑了我的帖子来展示在没有lambdas和匿名方法的情况下这是如何工作的。事实上,这是一个函数
,但是它与谓词
具有相同的方法签名……谢谢你的帮助。此外,第一个不起作用,但其他两个工作正常。它给出了此错误,“System.Array”不包含“AsEnumberable”的定义,并且找不到接受“System.Array”类型的第一个参数的扩展方法“AsEnumberable”(是否缺少using指令或程序集引用?)?尝试新列表{“c”、“b”、“a”}.OrderBy(s=>s)代码>如果代码无法编译,则必须检查usings和引用的程序集。
string[]' does not contain a definition for 'Count' and the best extension method
overload 'System.Linq.Enumerable.Count<TSource>
(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,bool>)'
has some invalid arguments
Argument 2: cannot convert from 'System.Predicate<string>' to
'System.Func<string,bool>'
int numberOfBooksWithLongNames = books.AsEnumberable().Count(s => longBooks(s));
int numberOfBooksWithLongNames = new List<string>(books).Count(s => longBooks(s));
int numberOfBooksWithLongNames = books.Count(s => longBooks(s));