c# – 每个循环都没有工作的Ienumerable concat

参见英文答案 > Is there a reason for C#’s reuse of the variable in a foreach?                                    5个
我正在尝试在每个循环中使用IEnumerable的Concat方法,但我无法使其正常工作.

IEnumerable<Geo> geos = null;
foreach (string a in values)
{
    if (geos == null)
        geos = entities.Geos.Where(g => (g.ACode == Convert.ToInt16(a)));
    else
        geos = geos.Concat(entities.Geos.Where(g => (g.ACode == Convert.ToInt16(a))));
}

它返回的只是值中最终“a”的值,对于值中存在的记录计数也是如此.

因此,如果我有1,2,3作为值,它只返回3.我也需要1,2和3的值.

我哪里错了?

解决方法

您可能正在使用旧版本的C#,在C#5(随Visual Studio 2013提供)中,他们改变了foreach的行为.在C#4中,a g ​​=> (g.ACode == Convert.ToInt16(a))将是lazely评估时foreach的最后一个值,在C#5和更新时它将始终是当前值.

要获得C#5行为,您只需要在foreach循环的范围内声明一个额外的变量,并在捕获中使用它.

IEnumerable<Geo> geos = null;
foreach (string a in values)
{
    string b = a;
    if (geos == null)
        geos = entities.Geos.Where(g => (g.ACode == Convert.ToInt16(b)));
    else
        geos = geos.Concat(entities.Geos.Where(g => (g.ACode == Convert.ToInt16(b))));
}

如果你很好奇,改变的是在C#4中,你的原始代码将被翻译成

IEnumerable<Geo> geos = null;
using(IEnumerator<string> enumerator = values.GetEnumerator())
{
    string a;
    while(enumerator.MoveNext())
    {
        a = enumerator.Current;

        if (geos == null)
            geos = entities.Geos.Where(g => (g.ACode == Convert.ToInt16(a)));
        else
            geos = geos.Concat(entities.Geos.Where(g => (g.ACode == Convert.ToInt16(a))));
    }
}

在C#5及更新版本中,它被翻译为

IEnumerable<Geo> geos = null;
using(IEnumerator<string> enumerator = values.GetEnumerator())
{
    while(enumerator.MoveNext())
    {
        string a = enumerator.Current;

        if (geos == null)
            geos = entities.Geos.Where(g => (g.ACode == Convert.ToInt16(a)));
        else
            geos = geos.Concat(entities.Geos.Where(g => (g.ACode == Convert.ToInt16(a))));
    }
}

通过做字符串b = a;在C#4中,我们重新创建了while循环内部的声明行为.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...