1

从“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;

这里的逻辑是什么?

4

6 回答 6

4

查询返回同一类对象的枚举。它可以是任何类,但它必须是所有行的一个。如果不将一对对象(城市和联系人姓名)组合成一个对象,则无法返回它们。在这种情况下,您将它们组合成一个具有两个属性的匿名类型对象。

附带说明一下,使用方法根本没有任何优势Select,因为这两个属性都来自同一个Customer类。实际上,效率低于原样返回客户对象,如下所示:

var query = from c in Customer.GetCustomers()
            where c.City == "Mexico D.F.";
foreach (var c in query)
{
    Console.WriteLine("{0} {1}", c.City, c.ContactName);
}
于 2012-07-09T02:44:35.053 回答
1

LINQ methods, like other C# methods, can only return one value/object. (There is no syntax support added for said hypothetical construct to magically do what the new { .. } stuff does.)

This is why the different fields are "wrapped" in an new object: it is then a single value/object with members representing all the selected fields.

(Likewise, the final result, an IEnumerable<..>, is also just one value/object with 0 or more elements in the sequence: in this case each element is a new object created with new { .. } above.)

于 2012-07-09T02:45:28.263 回答
0

new { c.City, c.ContactName}; is the C# syntax for an anonymous type.

于 2012-07-09T02:45:36.600 回答
0

The 'select' selects a Customer class, which consists of a City variable and a ContactNamr variable. So, you have to provide select with a new class with those variables

于 2012-07-09T02:45:50.807 回答
0

This is not SQL this is linq, and the syntax is basically what you see.

select new { c.City,  c.ContactName};

limits the output of the query to a type that contains just the City and the ContactName.

You may ask what type is this? Well it's an anonymous type that the compiler creates for you.

The code then iterates over the results of the query (IEnumerable<this_new_type>) and writes the ToString() of this new type.

于 2012-07-09T02:48:13.333 回答
0

select new is creating a new anonymous object.

"Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first"

select new {c.City, c.ContactName};

You could also create a concrete class to project into.

i.e a class called Blah:

class Blah
{
  public string City {get;set;}
  public string ContactName {get;set;}
}

... [rest of query]
select new Blah {c.City, c.ContactName};

The reason you cannot go "select c.City, c.ContactName;", is because the language syntax is not designed to handle it.

于 2012-07-09T02:51:21.497 回答