无法创建类型只有基本类型或枚举类型的恒定值在这方面的支持 [英] Unable to create a constant value of type Only primitive types or enumeration types are supported in this context

查看:167
本文介绍了无法创建类型只有基本类型或枚举类型的恒定值在这方面的支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到下面

这个错误的查询

  

无法创建类型的恒定值 API.Models.PersonProtocol 。只有基本类型或枚举类型在这方面的支持。


ppCombined 下面是 PersonProtocolType 的IEnumerable 对象,这是由2 CONCAT构建 PersonProtocol 列表。

这是为什么失败?我们不能使用LINQ 加入 SELECT 的内部条款加入

 变种人= db.Favorites
    。凡(X => x.userId ==用户id)
    。加入(db.Person中,x => x.personId,Y => y.personId,(X,Y)=>
        新PersonDTO
        {
            PERSONID = y.personId,
            addressId = y.addressId,
            favoriteId = x.favoriteId,
            personProtocol =(ICollection的< PersonProtocol>)ppCombined
                。凡(一个= GT; a.personId == x.personId)
                。选择(B = GT;新PersonProtocol()
                 {
                     personProtocolId = b.personProtocolId,
                     activateDt = b.activateDt,
                     PERSONID = b.personId
                 })
        });


解决方案

这不行,因为 ppCombined 是内存中的对象的集合,你不能加入一组数据在与另一组数据,是在存储器中的数据库中。你可以尝试,而不是提取内存中的 ppCombined 收集过滤项目 personProtocol 之后的已经从数据库中检索到的其它性质:

 变种人= db.Favorites
    。凡(F => f.userId ==用户id)
    。加入(db.Person,F => f.personId,P => p.personId,(F,P)=>
        新//匿名对象
        {
            PERSONID = p.personId,
            addressId = p.addressId,
            favoriteId = f.favoriteId,
        })
    .AsEnumerable()//数据库查询到此结束,剩下的就是在内存中查询
    。选择(X =>
        新PersonDTO
        {
            PERSONID = x.personId,
            addressId = x.addressId,
            favoriteId = x.favoriteId,
            personProtocol = ppCombined
                。凡(P => p.personId == x.personId)
                。选择(P =>新建PersonProtocol
                {
                    personProtocolId = p.personProtocolId,
                    activateDt = p.activateDt,
                    PERSONID = p.personId
                })
                .ToList()
        });

I am getting this error for the query below

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

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

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
                 })
        });

解决方案

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天全站免登陆