c# - LINQ:如何声明 IEnumerable[匿名类型]?

标签 c# .net linq

这是我的功能:

    private IEnumerable<string> SeachItem(int[] ItemIds)
    {
        using (var reader = File.OpenText(Application.StartupPath + @"\temp\A_A.tmp"))
        {
            var myLine = from line in ReadLines(reader)
                         where line.Length > 1
                         let id = int.Parse(line.Split('\t')[1])
                         where ItemIds.Contains(id)
                         let m = Regex.Match(line, @"^\d+\t(\d+)\t.+?\t(item\\[^\t]+\.ddj)")
                         where m.Success == true
                         select new { Text = line, ItemId = id, Path = m.Groups[2].Value };
            return myLine;
        }
    }

我得到一个编译错误,因为“myLine”不是一个 IEnumerable[string] 并且我不知道如何写 IEnumerable[Anonymous]

“无法将类型 'System.Collections.Generic.IEnumerable[AnonymousType#1]' 隐式转换为 'System.Collections.Generic.IEnumerable[string]'”

最佳答案

您不能声明 IEnumerable<AnonymousType>因为该类型在构建时没有(已知)名称。所以如果你想在函数声明中使用这个类型,就把它设为普通类型。或者只是修改您的查询以返回 IENumerable<String>并坚持使用该类型。

或返回IEnumerable<KeyValuePair<Int32, String>>使用以下选择语句。

select new KeyValuePair<Int32, String>(id, m.Groups[2].Value)

关于c# - LINQ:如何声明 IEnumerable[匿名类型]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/901854/

相关文章:

c# - Ninject 3 InRequestScope 不为同一请求返回同一实例

c# - Asp.net 中表单例份验证超时的单位是什么?

.NET:将程序集绑定(bind)到引用程序集的新版本,无需重新编译

c# - 简单的正则表达式问题 C#

javascript - 无法以 Angular 过滤日期

c# - 将 DirectShow 视频窗口附加到 WPF 控件

c# - 部署到 "C:\Program Files\..."后,我的应用程序无法更新 Access 数据库

c# - 建议的组合框下拉宽度

mysql - Entity Framework 将 StartsWith 转换为 MySQL 的 Locate,MySQL 的 Locate 不使用索引

c# - 如何在 linq 中将空列表视为空列表?