从“Essential LINQ”一书中检查以下程序:
class Customer
{
public string CustomerID {get;set;}
public string ContactName {get;set;}
public string City {get;set;}
public static List<Customer> GetCustomers()
{
return new List<Customer>
{
new Customer {CustomerID = "ALFKI", ContactName = "Maria Anders", City = "Berlin"},
new Customer {CustomerID = "ANATR", ContactName = "Sns Trujillo", City = "Mexico D.F."},
new Customer {CustomerID = "ANTON", ContactName = "Antonio Moreno", City = "Mexico D.F."}
};
}
}
void Main()
{
var query = from c in Customer.GetCustomers()
where c.City == "Mexico D.F."
select new { c.City, c.ContactName};
foreach (var cityandcontact in query)
{
Console.WriteLine(cityandcontact);
}
}
在 LINQ 查询中,如果我从行中删除“新”,为什么它会给我错误:
select new {c.City, c.ContactName};
为什么我们不能这样写
select c.City, c.ContactName;
这里的逻辑是什么?