【发布时间】:2009-03-06 09:28:05
【问题描述】:
为什么在下面的代码中会出现以下错误?
我想如果我将自定义对象放在其类型的通用列表中,那么 IEnumerable 会得到处理吗?我还需要对此 List 做什么才能在其上使用 LINQ?
不能隐式转换类型 'System.Collections.Generic.IEnumerable
<TestLinq23.Customer>' 到“TestLinq23.Customer”
using System;
using System.Collections.Generic;
using System.Linq;
namespace TestLinq23
{
class Program
{
static void Main(string[] args)
{
List<Customer> customerSet = new List<Customer>();
customerSet.Add(new Customer { ID = 1, FirstName = "Jim", LastName = "Smith" });
customerSet.Add(new Customer { ID = 2, FirstName = "Joe", LastName = "Douglas" });
customerSet.Add(new Customer { ID = 3, FirstName = "Jane", LastName = "Anders" });
Customer customerWithIndex = customerSet[1];
Console.WriteLine("Customer last name gotten with index: {0}", customerWithIndex.LastName);
Customer customerWithLinq = from c in customerSet
where c.FirstName == "Joe"
select c;
Console.WriteLine(customerWithLinq.LastName);
Console.ReadLine();
}
}
public class Customer
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
【问题讨论】:
标签: linq ienumerable