English 中文(简体)
在2起案件中的1起案件中,为什么有数字的<T>.Select()工作? 不能从使用中推断出
原标题:Why does the IEnumerable<T>.Select() works in 1 of 2 cases ? Can not be inferred from usage

收到<>信号:

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;
}
最佳回答

documentsOC.Add returns void.
It doesn t make any sense (and is impossible) to write .Select<Something, void>(...).

你试图做的事情首先不能奏效;Select<>/code>是zy,在你列举结果之前没有叫你。

您应定期使用<条码>。

问题回答

Error似乎是 当你从收集中挑选物品时,就没有填写返回声明。

例:

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

http://www.un? 如果是,没有办法预测到<代码>Func<> - only to an Action<T> - and Select想要Func<>

<代码>的返回类型是什么? ObservableCollection<Document> 添加。 通常,<代码>Add方法回归无效。 您可以不使用LINQSelect为所有内容执行程序,但只履行一种回报某种功能(/Select<>/code>的回报值来自何处?) 相反,可以使用LINQForEach或C# foreach loop。

选址为nota 替代 for。 而是使用:

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;




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...