Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq to sql linq表达式错误:无法显示列名_Linq To Sql_C# 4.0 - Fatal编程技术网

Linq to sql linq表达式错误:无法显示列名

Linq to sql linq表达式错误:无法显示列名,linq-to-sql,c#-4.0,Linq To Sql,C# 4.0,我使用的是LinqPad 4(nutshell数据库),我试图显示(Customer)名称,它是Customer表中的一列。 由于我现在遇到一个错误,如何在此查询中显示名称:不包含“名称”的定义 from p in Purchases join c in Customers on p.CustomerID equals c.ID group p by p.Date.Year into SalesPerYear select new { customername= SalesPerYear

我使用的是LinqPad 4(nutshell数据库),我试图显示(Customer)名称,它是Customer表中的一列。 由于我现在遇到一个错误,如何在此查询中显示名称:不包含“名称”的定义

   from p in Purchases 
join c in Customers on p.CustomerID equals c.ID
group p by p.Date.Year into SalesPerYear
select new {
customername= SalesPerYear.First().Name,
customerid= SalesPerYear.First().CustomerID,
totalsales= SalesPerYear.Sum(x=>x.Price)
}

您已经按CustomerID分组,因此它是分组键。i、 e.在查询中,您应该说:
customerid=SalesByYear.Key
。不确定年份从何而来。

试试这个

我假设您有一个包含(价格、客户ID和日期)列的采购表。

from p in Purchases  
group p by p.Date.Year into SalesByYear 
select new { 
customerid = SalesByYear.First().CustomerID, 
year=SalesByYear.Key, 
TotalVal = SalesByYear.Sum(g => g.Price) 
} 

是的,很有效,谢谢!我正在加入另一个表如何显示customer表中的名称?如果数据库中customer表和Purchase表之间有外键引用,则应该能够从Purchase导航到customer对象,因为外键已成为导航属性…类似于SalesByYear.First().customer.name。