简体   繁体   English

EntityFramework无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型

[英]EntityFramework Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context

I'm trying to do a ToDictionary() on my entities but I keep getting this error or another one similar like this one but with my entities shown in the message: 我正在尝试在我的实体上做一个ToDictionary() ,但是我一直收到这个错误或者另外一个像这样的错误但是我的实体显示在消息中:

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

Or this one with my entity in the error msg: 或者这个与我的实体在错误消息中:

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

I broke the query down to some smaller peace's but still getting either of these error msgs: 我把查询打破了一些较小的和平,但仍然得到这些错误消息:

var posts = dbContext
    .Posts
    .Where(x => channels.Contains(x.Connection))
    .DistinctBy(p => new { p.Medium, p.ID })
    .OrderByDescending(x => x.Date)
    .Skip(skip)
    .Take(take);

var asociatedTags = dbContext
    .PostTagRelation
    .Where(x => posts.Any(g => g.ItemId == x.ItemId && g.Medium == x.Medium)
        && x.Company == companyId)
    .Select(x => new { x.ItemId, x.Tags })
    .ToList();

Dictionary<string, Tags> dicTags = new Dictionary<string, Tags>();
dicTags = asociatedTags.ToDictionary(g => g.ItemId, g => g.Tags);

I came across a few posts about this, but I can't place them with my situation. 我发现了一些关于此事的帖子,但我不能把它们放在我的情况中。

Any help would really be appreciated! 真的很感激任何帮助!

DistinctBy (is it this one ?) is probably only an extension method for LINQ-to-Objects (ie for IEnumerable<T> , not for IQueryable<T> ). DistinctBy (是这个吗?)可能只是LINQ到对象的扩展方法(即IEnumerable<T> ,而不是IQueryable<T> )。 That means, calling it executes the DB query to this point and the result is a posts collection in memory (not an IQueryable<Post> ) which causes the exception in the second query at posts.Any... because with respect to the second SQL query posts is now a collection of "constant" objects which is not supported by LINQ-to-Entities. 这意味着,调用它执行数据库查询到这一点,结果是内存中的一个posts集合(不是一个IQueryable<Post> ),它导致posts.Any...中的第二个查询中的posts.Any...因为关于第二个SQL查询posts现在是LINQ-to-Entities不支持的“常量”对象的集合。 Moreover it causes that sorting, Skip and Take are performed in memory and not in the database with possible unnecessary overhead and much more loaded data than you need. 此外,它导致排序, SkipTake在内存中执行,而不是在数据库中执行,可能有不必要的开销和比您需要的更多的加载数据。

You can try to avoid DistinctBy and replace it by the following which should return posts as IQueryable<Post> : 您可以尝试避免使用DistinctBy并将其替换为以下应该将posts作为IQueryable<Post>返回:

var posts = dbContext
    .Posts
    .Where(x => channels.Contains(x.Connection))
    .GroupBy(p => new { p.Medium, p.ID })
    .Select(g => g.FirstOrDefault()) // gives the first Post in each group
    .OrderByDescending(x => x.Date)
    .Skip(skip)
    .Take(take);

Do the ToList() call before the creation of the anonymous class ( Select(x => new { x.ItemId, x.Tags }) ) 在创建匿名类之前执行ToList()调用( Select(x => new { x.ItemId, x.Tags })

var dicTags= dbContext.PostTagRelation
   .Where(x => posts.Any(g => g.ItemId == x.ItemId && g.Medium == x.Medium)
       && x.Company == companyId)
   //force execution of the query
   .ToList() 
   //now you have an IEnumerable instead of IQueryable
   .ToDictionary(g => g.ItemId, g => g.Tags);

暂无
暂无

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

相关问题 EF错误无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型 - EF error Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context 无法创建“匿名类型”类型的常量值。 在此上下文中仅支持基元类型或枚举类型 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型。 在Linq C#中 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context. in Linq C# 无法创建类型为“匿名类型”的常量值。 在此上下文中仅支持原始类型或枚举类型两个db Linq查询 - Unable to create a constant value of type 'Anonymous type'. Only primitive types or enumeration types are supported in this context two db Linq query UOW模式-无法创建类型的常量值在此上下文中仅支持基本类型或枚举类型 - UOW pattern - Unable to create a constant value of type Only primitive types or enumeration types are supported in this context C# - Linq:无法创建类型的常量值在此上下文中仅支持基本类型或枚举类型。 - C# - Linq : Unable to create a constant value of type Only primitive types or enumeration types are supported in this context. LINQ,无法创建XXX类型的常量值。在此上下文中仅支持基元类型或枚举类型 - LINQ, Unable to create a constant value of type XXX. Only primitive types or enumeration types are supported in this context 错误:无法创建类型为“ System.Object”的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context LINQ查询错误:无法创建类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - LINQ Query error: Unable to create a constant value of type. Only primitive types or enumeration types are supported in this context 错误:无法创建类型的常量值。 在此上下文中仅支持原始类型或枚举类型 - Error: Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM