Home > Backend Development > C++ > Why Does My LINQ Query Fail with 'Unable to create a constant value of type…'?

Why Does My LINQ Query Fail with 'Unable to create a constant value of type…'?

Linda Hamilton
Release: 2025-01-17 23:21:11
Original
113 people have browsed it

Why Does My LINQ Query Fail with

LINQ query error: "Cannot create a constant value of type API.Models.PersonProtocol. Only primitive or enumerated types are supported in this context"

This article will resolve an error in a LINQ query caused by trying to create a constant value of type API.Models.PersonProtocol in a SELECT clause. Only primitive or enumerated types are allowed as constants in this context.

The problem is with the following line of code:

<code class="language-csharp">personProtocol = (ICollection<personprotocol>) ppCombined
    .Where(a => a.personId == x.personId)
    .Select( b => new PersonProtocol()
     {
         personProtocolId = b.personProtocolId,
         activateDt = b.activateDt,
         personId = b.personId
     })</code>
Copy after login

Here, ppCombined is a collection of PersonProtocolType objects in memory. It cannot connect directly with data retrieved from the database.

Solution

To resolve this error, the filtering and mapping operations of ppCombined need to be moved outside the SELECT clause. The following is the modified code:

<code class="language-csharp">var persons = db.Favorites
    .Where(f => f.userId == userId)
    .Join(db.Person, f => f.personId, p => p.personId, (f, p) =>
        new // 匿名对象
        {
            personId = p.personId,
            addressId = p.addressId,   
            favoriteId = f.favoriteId,
        })
    .AsEnumerable() // 数据库查询在此结束,其余部分是内存中的查询
    .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()
        });</code>
Copy after login

In this modified query, the ppCombined collection is first filtered and mapped in memory to get the required PersonProtocol objects. The result is then assigned to the PersonDTO object’s personProtocol property. Avoiding the use of in-memory collections in database queries by moving the processing of ppCombined after AsEnumerable(). AsEnumerable() Convert the database query results into a memory collection, and all subsequent operations are performed in memory, thus solving the original error.

The above is the detailed content of Why Does My LINQ Query Fail with 'Unable to create a constant value of type…'?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template