我从我的一个 friend 那里得到了一些代码,它在Windows中效果很好。表格申请。
当我尝试在Xamarin.Forms项目中使用相同的代码时,它说:

System.Collections.Generic.List>'没有为'ForEach'定义。 [...](也许缺少“使用中”)(德语翻译)

我有:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Xamarin.Forms;
using System.Reflection;

这是给出错误的代码:
public Company GetCompanyById(int companyId) {
        Company company = new Company();

        allContinents
             .MyContinents
             .Select(x => x.Countries)
             .ToList()
             .ForEach(countryList => {
                 countryList.ForEach(country => {
                     country.Cities.ForEach(city => {
                         city.Companies.ForEach(com => {
                             if (com.Id.Equals(companyId))
                                 company = com;
                         });
                     });
                 });
             });
        return company;
    }

为什么它不像Windows.forms应用程序那样工作?
ps:这是第一个带有蓝色下划线的ForEach

谢谢

最佳答案

您正在使用.NET的可移植类库子集;不包含List<T>.ForEach

我个人并不热衷于这种方法-使用LINQ选择合适的公司会更具可读性。毕竟,您正在执行查询...

return allContinents.MyContinents
                    .SelectMany(x => x.Countries)
                    .SelectMany(c => c.Cities)
                    .SelectMany(c => c.Companies)
                    .First(c => c.Id == companyId);

关于c# - List(T).ForEach未使用Xamarin定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26503341/

10-11 22:35
查看更多