NotSupportedException:无法创建类型的常量值...在此上下文中仅支持基元类型或枚举类型。 [英] NotSupportedException: Unable to create a constant value of type... Only primitive types or enumeration types are supported in this context.

查看:202
本文介绍了NotSupportedException:无法创建类型的常量值...在此上下文中仅支持基元类型或枚举类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个链接语句,它抛出NotSupportedException并带有以下消息:



无法创建"ACM.RiskAlive.Core.Data.AverageSafeRecProfile"类型的常量值。在此上下文中仅支持原始类型或枚举类型。



这是我的代码:



var facilityTypeId = _context.Projects.Where(x => x.ProjectId == projectId).Select(x => x.Facility.TypeCodeId).First();



var consq = projectIdsForAverage == null

                ? _context.ConsequencesByProject(x => x.ProjectId!= projectId&& x.Facility.TypeCodeId == facilityTypeId)

                :_context.ConsequencesByProject(x => projectIdsForAverage.Contains(x.ProjectId));
$


var recs = _context.GetAverageRecommendationProfile(projectId,projectIdsForAverage.ToCommaSeperated()) ;
$


尝试{

var dto = consq.Select( x =>新郎
     {

     ClientCode = x.Category.HazopCode,

         ChartLabel = x.Category.Label,

         ClientColor = x.Category.Color,

         Category = x.Category.DataMiningCode.Name,

         Color = x.Category.DataMiningCode.Color,

         Credit = recs.Where(r => r.ConsequenceId == x.Con sequenceId).Sum(r => r.NonLowCreditTotal)

                })
                    .Where(x => x.Category!= null&& x.ClientCode!= null)

                    .GroupBy(x => new {x.Category,x.Color})

                    。选择(x =>新ChartDataDto

           {

              Label = x.Key.Category,

              Color = x.Key.Color,

            值= x.Sum(y => y.Credit)

          });



                return dto.ToList();

        }¥b $ b        catch(例外e)

        {

               输入t = e.GetType();

               返回null;

        } b
$
基本上,我有两组数据:后果(consq)和建议(recs)。在我分配给dto的linq语句中,我在第一个Select语句中创建了一个新对象,并将新
对象的Credit字段分配给在这些对象中找到的NonLowCreditTotal的总和。结果ID与当前结果的后果ID匹配的建议。然后我尝试将dto转换为List,然后抛出异常。



任何人都可以看到我做错了什么。谢谢。

Hello,

I have a link statement that's throwing a NotSupportedException with the following message:

Unable to create a constant value of type 'ACM.RiskAlive.Core.Data.AverageSafeRecProfile'. Only primitive types or enumeration types are supported in this context.

Here's my code:

var facilityTypeId = _context.Projects.Where(x => x.ProjectId == projectId).Select(x => x.Facility.TypeCodeId).First();

var consq = projectIdsForAverage == null
                ? _context.ConsequencesByProject(x => x.ProjectId != projectId && x.Facility.TypeCodeId == facilityTypeId)
                : _context.ConsequencesByProject(x => projectIdsForAverage.Contains(x.ProjectId));

var recs = _context.GetAverageRecommendationProfile(projectId, projectIdsForAverage.ToCommaSeperated());

try {
var dto = consq.Select(x => new
        {
        ClientCode = x.Category.HazopCode,
                ChartLabel = x.Category.Label,
                ClientColor = x.Category.Color,
                Category = x.Category.DataMiningCode.Name,
                Color = x.Category.DataMiningCode.Color,
                Credit = recs.Where(r => r.ConsequenceId == x.ConsequenceId).Sum(r => r.NonLowCreditTotal)
                })
                    .Where(x => x.Category != null && x.ClientCode != null)
                    .GroupBy(x => new { x.Category, x.Color })
                    .Select(x => new ChartDataDto
                    {
                        Label = x.Key.Category,
                        Color = x.Key.Color,
                        Value = x.Sum(y => y.Credit)
                    });

                return dto.ToList();
        }
        catch (Exception e)
        {
                Type t = e.GetType();
                return null;
        }

Essentially, I have two sets of data: consequences (consq) and recommendations (recs). In the linq statement which I assign to dto, I create a new object out of the consequences in the first Select statement, and I assign the Credit field of the new object to the sum of the NonLowCreditTotal found in those recommendations where the consequence ID matches the consequence ID of the current consequence. Then I try to convert dto to a List and that's when the exception is thrown.

Can anyone see what I'm doing wrong. Thank you.

推荐答案

无法创建"ACM.RiskAlive.Core.Data.AverageSafeRecProfile"类型的常量值。在此上下文中仅支持原始类型或枚举类型。

Unable to create a constant value of type 'ACM.RiskAlive.Core.Data.AverageSafeRecProfile'. Only primitive types or enumeration types are supported in this context.

它告诉你Linq无法做到的错误,你不能在Linq查询中使用类型"ACM.RiskAlive.Core.Data.AverageSafeRecProfile"。你可以使用 原始类型,如int,string,double,或者具有枚举的东西,可以像集合等一样枚举
等。

It's telling you what is wrong Linq can't do it, and you can't use type 'ACM.RiskAlive.Core.Data.AverageSafeRecProfile' in the Linq query. You can use  primitive types like int, string, double,  or something that has a enumeration than can be enumerated over like a collection etc, etc.


这篇关于NotSupportedException:无法创建类型的常量值...在此上下文中仅支持基元类型或枚举类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆