0

我有一个初始查询,我想修改它以增加结果的粒度。但是 Visual Studio 告诉我我的查询无效,我不明白为什么。基本上我想根据 2 个属性(列)对我的数据进行分组,并按前 N 个字符对其中一个属性进行分组。

有效的初始查询:

List<PostalCode> codes = (from customer in bd.Customers
                        group customer by customer.postalcode.Substring(0, postalCodeLength) into postalCodes
                        select new PostalCode
                        {
                            Postal = postalCodes.Key,
                            Count = postalCodes.Count()
                        }).ToList();
                return codes;

由 ** 标记的查询被 VS2010 标记为错误:

List<PostalCode> codes = (from customer in bd.Customers
                          group customer by new { **customer.postalcode.Substring(0, postalCodeLength)**, customer.CustomerGroupType}
                          into postalCodes
                          select new PostalCode 
                          { 
                                Postal = postalCodes.Key.postalcode,
                                CustomerGroupType = postalCodes.Key.CustomerGroupType,
                                Count = postalCodes.Count() 
                          }).ToList();
 return codes;
4

1 回答 1

2

新的 { } 对象语法要求属性具有名称 - 您的原始查询不需要这些名称。它无法从您的方法调用中推断出名称。所以我建议将其更改为:

from customer in bd.Customers
group customer by new { TrimmedPostalCode = customer.postalcode.Substring(0, postalCodeLength), customer.CustomerGroupType}
into postalCodes
select new PostalCode 
{ 
    Postal = postalCodes.Key.TrimmedPostalCode,
    CustomerGroupType = postalCodes.Key.CustomerGroupType,
    Count = postalCodes.Count() 
}
于 2013-06-05T00:13:23.143 回答