这是我的功能:
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/