我正在尝试对我的实体执行
ToDictionary()
,但我一直收到此错误或类似此错误的另一个错误,但是在消息中显示了我的实体:
或者这个带有我的实体的错误消息:无法创建类型为“匿名类型”的常量值。仅支持基元类型或枚举类型。
我将查询分解成更小的部分,但仍然收到这些错误消息之一。无法创建类型为“DataAccess.Posts”的常量值。仅支持基元类型或枚举类型。
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);
我看到了一些关于这个问题的帖子,但是它们与我的情况不符。
非常感谢任何帮助!
.DistinctBy()
据我所知不是一个开箱即用的方法。谷歌说它随 MoreLinq 一起提供。 - MarcoGroupBy(p => new { p.Medium, p.ID }).Select(g => g.FirstOrDefault())
。 - SlaumaGroupBy()
之后,我无法按x.Date排序,因为我会收到一个编译器错误,指出“System.Linq.IGrouping.......不包含'Date'的定义,也没有扩展方法......你是否缺少引用?” - Yustme