简体   繁体   English

LINQ错误:GroupBy选择上出现System.NotSupportedException

[英]LINQ error: System.NotSupportedException on GroupBy Selection

When var items = q3.ToList(); var items = q3.ToList(); executes from the code snippet below, it throws exception System.NotSupportedException. 从下面的代码段执行,它将引发异常System.NotSupportedException。 The aim is to get the list of items after the grouping. 目的是获得分组后的items列表。

Exception: Unable to create a constant value of type 'AppDB.Stage.Rules'. Only primitive types or enumeration types are supported in this context. 例外: Unable to create a constant value of type 'AppDB.Stage.Rules'. Only primitive types or enumeration types are supported in this context. Unable to create a constant value of type 'AppDB.Stage.Rules'. Only primitive types or enumeration types are supported in this context.

  var valuations = context.stage
                .Where(q => q.stageID == stageID && !rules.ToList().Any(r => r.type1 == q.type1 || r.type2 == q.type2))
               .GroupBy(q => q.stageKey)
               .Select(g => g) ;

            var q3 = valuations.Select(y => new StageType
            {
                TypeKey = y.Key,
                TypeName= "UNKNOWN",
            });
            var items = q3.ToList(); //error here

Your database doesn't have any idea of what your in-memory rules actually is, and in-turn cant convert this statement to SQL 您的数据库不知道您的内存rules实际上是什么,因此无法将该语句转换为SQL

The simplest solution will be to leave it as an IQueriable and don't use ToList , 最简单的解决方案是将其保留为IQueriable并且不使用ToList

context.stage
       .Where(q => q.stageID == stageID && !rules.Any(r => r.type1 == q.type1 || r.type2 == q.type2))
       .GroupBy(q => q.stageKey)
       .Select(g => g) ;

However , if it is already in memory, then you will have to send the values as a primitive list 但是 ,如果它已经在内存中,则必须将值作为原始列表发送

var type1s = rules.Select(x => x.type1);
var type2s = rules.Select(x => x.type2);

context.stage
       .Where(q => q.stageID == stageID && !type1s.Contains(q.type1) && !type2s.Contains(q.type2))
       .GroupBy(q => q.stageKey)
       .Select(g => g) ;

Because rules.ToList() makes results in memory, you can't use it inside an IQueriable that executes over SQL. 因为rules.ToList()在内存中产生结果,所以不能在通过SQL执行的IQueriable中使用它。 You should first bring your data into memory and then narrow it by other in-memory object. 您应该先将数据放入内存,然后再通过其他内存中对象缩小数据范围。

 var valuations = context.stage.ToList()
                .Where(q => q.stageID == stageID && !rules.ToList().Any(r => r.type1 == q.type1 || r.type2 == q.type2))
               .GroupBy(q => q.stageKey)
               .Select(g => g) ;

            var q3 = valuations.Select(y => new StageType
            {
                TypeKey = y.Key,
                TypeName= "UNKNOWN",
            });
            var items = q3.ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ GroupBy子句System.NotSupportedException错误 - LINQ GroupBy Clause System.NotSupportedException error linq“System.NotSupportedException”出错 - There is an error with linq “System.NotSupportedException” 按日期时间过滤 LINQ 查询导致 System.NotSupportedException 错误 - Filtering a LINQ query by DateTime causing System.NotSupportedException Error System.NotSupportedException使用DateTime吗? 在Linq to实体 - System.NotSupportedException using DateTime? in Linq to Entities LINQ嵌套查询问题-System.NotSupportedException - LINQ nested query issue - System.NotSupportedException 发生System.NotSupportedException - System.NotSupportedException occuring WebRequest System.NotSupportedException - WebRequest System.NotSupportedException Linq to EF系统不支持的异常System.NotSupportedException - Linq to EF system not supported exception System.NotSupportedException System.NotSupportedException:'LINQ to Entities中不支持指定的类型成员'StartDateTime' - System.NotSupportedException: 'The specified type member 'StartDateTime' is not supported in LINQ to Entities 在LINQ to Entities中使用中间实体会导致System.NotSupportedException - Using an intermediate entity in LINQ to Entities leads to System.NotSupportedException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM