c# - Ienumerable concat 不适用于每个循环

标签 c# linq c#-4.0

<分区>

我试图在 for each 循环中使用 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 中,g => (g.ACode == Convert.ToInt16(a)) 中的 a 将是 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))));
    }
}

通过在 C# 4 中执行 string b = a;,我们重新创建了声明在 while 循环内的行为。

关于c# - Ienumerable concat 不适用于每个循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39294384/

相关文章:

c# - 如何为泛型创建一个完全懒惰的单例

c# - 删除文件 - 跳过正在使用的文件

c# - 处理最小化程序

用于 List<object> 的 c# JSON.Net NullValueHandling

c# - 工厂模式 : Restrict object construction to factory

c# - LINQ First/FirstOrDefault 是否迭代整个可枚举?

c# - Linq 包含对字符串的检查有误检测

c# - 如何将 System.Linq.Enumerable.WhereListIterator<int> 转换为 List<int>?

c# - 使用 Parallel for each 获取堆栈溢出异常,这是由于线程安全吗?

c# - 如何在 C# 中声明枚举列表<>