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

发布于 2024-10-10 22:07:13 字数 1184 浏览 3 评论 0原文

我收到此错误消息:

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

灼痛 2024-10-17 22:07:13

documentsOC.Add 返回 void
编写 .Select(...) 没有任何意义(也是不可能的)。

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

您应该使用常规的 foreach 循环。

documentsOC.Add returns void.
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.

小傻瓜 2024-10-17 22:07:13

当您从集合中选择项目时,错误似乎缺少 Return 语句。

例子:

collection = nonLabors.Select(item =>
                {
                    item.Travel_Miles = item.Travel_Miles_Original != null ? decimal.Parse(item.Travel_Miles_Original) : 0;
                    return item;
                }).ToList();

The Error seems The Return statement missing when you select an Items from the collection.

Example:

collection = nonLabors.Select(item =>
                {
                    item.Travel_Miles = item.Travel_Miles_Original != null ? decimal.Parse(item.Travel_Miles_Original) : 0;
                    return item;
                }).ToList();
影子的影子 2024-10-17 22:07:13

怀疑 Add 返回 void - 是这样吗?如果是这样,则无法将其投影到 Func<,> - 只能投影到 Action - 并且 Select 想要函数<,>

Select 与间接 `foreach 不同

I suspect that Add returns void - is that right? If so there is no way of projecting that to a Func<,> - only to an Action<T> - and Select wants the Func<,>.

Select is not the same as an indirect `foreach

等往事风中吹 2024-10-17 22:07:13

ObservableCollection.Add 的返回类型是什么?通常,Add 方法返回 void。您不能使用 LINQ Select 为所有元素执行过程,只能使用返回某些内容的函数(否则 Select 的返回值从何而来?)。相反,您可以使用 LINQ ForEach 或 C# foreach 循环。

What is the return type of ObservableCollection<Document>.Add? Typically an Add method returns void. You can't use LINQ Select to execute a procedure for all elements, only a function that returns something (else where would the return value of Select come from?). Instead you can use LINQ ForEach or the C# foreach loop.

心安伴我暖 2024-10-17 22:07:13

Select不是 foreach 的替代品。使用这个代替:

ObservableCollection<Document> documentsOC = new ObservableCollection<Document>();
IEnumerable<Document> documents = _docRepo.GetDocumentsByPupilId(_selectedPupil.Id);
foreach(var doc in documents)
{
    documentsOC.Add(doc);
}
SelectedPupil.Documents.DocumentList = documentsOC;

Select is not a replacement for a foreach. Use this instead:

ObservableCollection<Document> documentsOC = new ObservableCollection<Document>();
IEnumerable<Document> documents = _docRepo.GetDocumentsByPupilId(_selectedPupil.Id);
foreach(var doc in documents)
{
    documentsOC.Add(doc);
}
SelectedPupil.Documents.DocumentList = documentsOC;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文