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

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

问题描述

我在下面的查询中收到此错误

I am getting this error for the query below

无法创建 API.Models.PersonProtocol 类型的常量值.此上下文中仅支持原始类型或枚举类型

Unable to create a constant value of type API.Models.PersonProtocol. Only primitive types or enumeration types are supported in this context

下面的

ppCombined是一个PersonProtocolTypeIEnumerable对象,由2个PersonProtocol列表的concat构成.

ppCombined below is an IEnumerable object of PersonProtocolType, which is constructed by concat of 2 PersonProtocol lists.

为什么会失败?我们不能在 JOINSELECT 内使用 LINQ JOIN 子句吗?

Why is this failing? Can't we use LINQ JOIN clause inside of SELECT of a JOIN?

var persons = db.Favorites
    .Where(x => x.userId == userId)
    .Join(db.Person, x => x.personId, y => y.personId, (x, y) =>
        new PersonDTO
        {
            personId = y.personId,
            addressId = y.addressId,                   
            favoriteId = x.favoriteId,
            personProtocol = (ICollection<PersonProtocol>) ppCombined
                .Where(a => a.personId == x.personId)
                .Select( b => new PersonProtocol()
                 {
                     personProtocolId = b.personProtocolId,
                     activateDt = b.activateDt,
                     personId = b.personId
                 })
        });

推荐答案

这行不通,因为 ppCombined 是内存中的对象集合,您不能将数据库中的一组数据与另一组数据连接起来内存中的数据.您可以尝试在内存中提取 ppCombined 集合的过滤项 personProtocol 从数据库中检索其他属性:>

This cannot work because ppCombined is a collection of objects in memory and you cannot join a set of data in the database with another set of data that is in memory. You can try instead to extract the filtered items personProtocol of the ppCombined collection in memory after you have retrieved the other properties from the database:

var persons = db.Favorites
    .Where(f => f.userId == userId)
    .Join(db.Person, f => f.personId, p => p.personId, (f, p) =>
        new // anonymous object
        {
            personId = p.personId,
            addressId = p.addressId,   
            favoriteId = f.favoriteId,
        })
    .AsEnumerable() // database query ends here, the rest is a query in memory
    .Select(x =>
        new PersonDTO
        {
            personId = x.personId,
            addressId = x.addressId,   
            favoriteId = x.favoriteId,
            personProtocol = ppCombined
                .Where(p => p.personId == x.personId)
                .Select(p => new PersonProtocol
                {
                    personProtocolId = p.personProtocolId,
                    activateDt = p.activateDt,
                    personId = p.personId
                })
                .ToList()
        });

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

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