Đây là chức năng của tôi:LINQ: Cách khai báo IEnumerable [AnonymousType]?
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;
}
}
tôi nhận được một lỗi biên dịch, bởi vì "myLine" không phải là một IEnumerable [chuỗi] và tôi không biết làm thế nào để viết IEnumerable [Anonymous]
"Can not chuyển đổi hoàn toàn loại 'System.Collections.Generic.IEnumerable [AnonymousType # 1]' thành 'System.Collections.Generic.IEnumerable [string]' "
Khi bạn nói "select new {...}", bạn đã tạo một AnonymousType. –
Không, nhưng bạn có thể trả về IEnumerable và sử dụng thủ thuật CastByExample.Không được đề nghị mặc dù –