微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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循环内部的声明行为.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐