为什么 IEnumerable.Select() 在两种情况之一中起作用?无法从使用情况推断

2023-11-23

我明白了错误信息:

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.

第一种方法使用a没有问题IEnumerable<T>.Select()? 第二种方法的问题出在哪里呢?

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;
}

documentsOC.Add回报void.
写没有任何意义(也是不可能的).Select<Something, void>(...).

你想做的事一开始就行不通;Select是惰性的,在枚举结果之前不会调用您的函数。

你应该使用常规的foreach loop.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么 IEnumerable.Select() 在两种情况之一中起作用?无法从使用情况推断 的相关文章

随机推荐