List(T).ForEach未使用Xamarin定义 [英] List(T).ForEach is not defined using Xamarin

查看:395
本文介绍了List(T).ForEach未使用Xamarin定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I got some code from a friend of mine and it works great in a windows. forms application. When I try to use the same Code in a Xamarin.Forms project, it says:

System.Collections.Generic.List>'没有为'ForEach'定义. [...](可能缺少使用中")(德语翻译)

System.Collections.Generic.List>' has no definition for 'ForEach'. [...] (maybe a "using" is missing) (translated from german))

我有:

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;

以下是给出错误的代码:

Here is the Code that gives the error:

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

Why doesn't it work as is does in a windows.forms app? ps: it's the first ForEach with is underlined in blue

谢谢

推荐答案

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

You're using the Portable Class Library subset of .NET; that doesn't include List<T>.ForEach.

我个人并不热衷于这种方法-使用LINQ选择合适的公司会更加容易理解.毕竟,您正在执行查询 ...

Personally I'm not keen on this approach anyway - it would be much more readable IMO to use LINQ to select the right company. After all, you're performing a query...

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

这篇关于List(T).ForEach未使用Xamarin定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆