为什么 IEnumerable.Select() 在两种情况之一中起作用?无法从使用情况推断
我收到此错误消息:
The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
第一种方法使用 IEnumerable
没有问题? 第二种方法的问题出在哪里呢?
private void GetPupilsForSchoolclass()
{
ObservableCollection<PupilViewModel> pupilsOC = new ObservableCollection<PupilViewModel>
( _adminRepo.GetPupilsBySchoolclassId(_selectedSchoolclass.SchoolclassId).Select(p => new PupilViewModel(p, _adminRepo))
);
SelectedSchoolclass.PupilListViewModel = pupilsOC;
}
private void GetDocumentsForPupil()
{
ObservableCollection<Document> documentsOC = new ObservableCollection<Document>();
IEnumerable<Document> documents = _docRepo.GetDocumentsByPupilId(_selectedPupil.Id);
documents.Select(doc => documentsOC.Add(doc));
SelectedPupil.Documents.DocumentList = documentsOC;
}
I get this error message:
The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,int,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
The first method has no problems using a IEnumerable<T>.Select()
?
Where is the problem with the 2nd method?
private void GetPupilsForSchoolclass()
{
ObservableCollection<PupilViewModel> pupilsOC = new ObservableCollection<PupilViewModel>
( _adminRepo.GetPupilsBySchoolclassId(_selectedSchoolclass.SchoolclassId).Select(p => new PupilViewModel(p, _adminRepo))
);
SelectedSchoolclass.PupilListViewModel = pupilsOC;
}
private void GetDocumentsForPupil()
{
ObservableCollection<Document> documentsOC = new ObservableCollection<Document>();
IEnumerable<Document> documents = _docRepo.GetDocumentsByPupilId(_selectedPupil.Id);
documents.Select(doc => documentsOC.Add(doc));
SelectedPupil.Documents.DocumentList = documentsOC;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
documentsOC.Add
返回void
。编写
.Select(...)
没有任何意义(也是不可能的)。你想做的事一开始就行不通;
Select
是惰性的,在枚举结果之前不会调用您的函数。您应该使用常规的
foreach
循环。documentsOC.Add
returnsvoid
.It doesn't make any sense (and is impossible) to write
.Select<Something, void>(...)
.What you're trying to do cannot work in the first place;
Select
is lazy and doesn't call your function until you enumerate the results.You should use a regular
foreach
loop.当您从集合中选择项目时,错误似乎缺少 Return 语句。
例子:
The Error seems The Return statement missing when you select an Items from the collection.
Example:
我怀疑
Add
返回void
- 是这样吗?如果是这样,则无法将其投影到Func<,>
- 只能投影到Action
- 并且Select
想要函数<,>
。Select
与间接 `foreach 不同I suspect that
Add
returnsvoid
- is that right? If so there is no way of projecting that to aFunc<,>
- only to anAction<T>
- andSelect
wants theFunc<,>
.Select
is not the same as an indirect `foreachObservableCollection.Add
的返回类型是什么?通常,Add
方法返回 void。您不能使用 LINQSelect
为所有元素执行过程,只能使用返回某些内容的函数(否则Select
的返回值从何而来?)。相反,您可以使用 LINQForEach
或 C# foreach 循环。What is the return type of
ObservableCollection<Document>.Add
? Typically anAdd
method returns void. You can't use LINQSelect
to execute a procedure for all elements, only a function that returns something (else where would the return value ofSelect
come from?). Instead you can use LINQForEach
or the C# foreach loop.Select不是 foreach 的替代品。使用这个代替:
Select is not a replacement for a foreach. Use this instead: