加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:35:09 所属栏目:百科 来源:网络整理
导读:参见英文答案 Is there a reason for C#’s reuse of the variable in a foreach?????????????????????????????????????5个 我正在尝试在每个循环中使用IEnumerable的Concat方法,但我无法使其正常工作. IEnumerableGeo geos = null;foreach (string a in val
参见英文答案 > 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循环内部的声明行为.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读