Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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 对一组匿名类型执行Sum():可能吗?_Linq_Entity Framework_Anonymous Types - Fatal编程技术网

Linq 对一组匿名类型执行Sum():可能吗?

Linq 对一组匿名类型执行Sum():可能吗?,linq,entity-framework,anonymous-types,Linq,Entity Framework,Anonymous Types,如果我从查询中选择匿名类型(select new): var smsPerGroup = from g in db.Groups select new { GroupName = g.Name, ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count()

如果我从查询中选择匿名类型(
select new
):

 var smsPerGroup = from g in db.Groups
                   select new
                   {
                       GroupName = g.Name,
                       ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count()
                   };
碰巧我不能在上面求和():

引发的错误是: 分析查询时出错。

最正确的解决方案是什么?

我不想在这个查询中创建一个只使用一次的类

是否可能有“不太匿名”类型?这是:定义其属性的类型,而不是类本身的类型

类似这样的:(这在语法上是不正确的)




编辑:详细错误如下

There was an error parsing the query. [ Token line number = 6,Token line offset = 5,Token in error = SELECT ]

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlServerCe.SqlCeException: There was an error parsing the query. [ Token line number = 6,Token line offset = 5,Token in error = SELECT ]

Source Error: 


Line 159:                                      };
Line 160:
Line 161:                    int max = smsPerGroup.Max(g => g.ReceivedSMS);

一个简单的解决方案是在调用.Sum()之前用.ToList()具体化查询:


如果这是EF(或任何提供程序),请添加适当的标记。并非所有LINQ都是平等的。还要确保包含完整的消息-这应该包括异常的类型。我看不出您的查询有任何错误。你能再描述一下你的错误吗?是运行时错误吗?是否引发了任何异常?@MarcinJuraszek这是一条可从SQL Server查询生成的异常消息。。但是如果OP这样说并包含完整的细节,那就太好了。我用细节添加了错误。这是EF,它成功了!他担心这会推迟质询,但做得很好
    ...
   select new
   {
       string GroupName = g.Name,
       int ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count()
   };
There was an error parsing the query. [ Token line number = 6,Token line offset = 5,Token in error = SELECT ]

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlServerCe.SqlCeException: There was an error parsing the query. [ Token line number = 6,Token line offset = 5,Token in error = SELECT ]

Source Error: 


Line 159:                                      };
Line 160:
Line 161:                    int max = smsPerGroup.Max(g => g.ReceivedSMS);
var test = (from g in db.Groups
            select new
            {
                GroupName = g.Name,
                ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count()
            }).ToList();

var sum = test.Sum(t => t.ReceivedSMS);