© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
14 replies
Utsuhoagie

LINQ expression could not be translated after converting object

public static BugDTO ConvertToDTO(Bug bug) => new BugDTO
{
    Id = bug.Id,
    Description = bug.Description,
    IsFixed = bug.IsFixed
};

// GET: api/Bugs
//    api/Bugs?isFixed={true|false}
[HttpGet]
public async Task<ActionResult<IEnumerable<Bug /* BugDTO */>>> GetBugs([FromQuery] bool? isFixed)
{
    // var allBugDTO = _context.Bugs.Select(bug => ConvertToDTO(bug));
    var allBugDTO = _context.Bugs.Select(bug => bug);

    if (isFixed == null)
    {
        var allBugDTOList = await allBugDTO.ToListAsync();
        return allBugDTOList;
    }

    var filteredBugDTOList = await allBugDTO.Where(bug => bug.IsFixed == isFixed!).ToListAsync();
    return filteredBugDTOList;
}        
public static BugDTO ConvertToDTO(Bug bug) => new BugDTO
{
    Id = bug.Id,
    Description = bug.Description,
    IsFixed = bug.IsFixed
};

// GET: api/Bugs
//    api/Bugs?isFixed={true|false}
[HttpGet]
public async Task<ActionResult<IEnumerable<Bug /* BugDTO */>>> GetBugs([FromQuery] bool? isFixed)
{
    // var allBugDTO = _context.Bugs.Select(bug => ConvertToDTO(bug));
    var allBugDTO = _context.Bugs.Select(bug => bug);

    if (isFixed == null)
    {
        var allBugDTOList = await allBugDTO.ToListAsync();
        return allBugDTOList;
    }

    var filteredBugDTOList = await allBugDTO.Where(bug => bug.IsFixed == isFixed!).ToListAsync();
    return filteredBugDTOList;
}        


Why am I getting this error when I try to convert an object to another very similar object? This is in ASP.NET with Web API scaffolding, in my
BugController.cs
BugController.cs
class.
Bug
Bug
and
BugDTO
BugDTO
are almost identical classes, except
Bug
Bug
has an extra property.
If I run the code below, it works just fine. But if I change the return type from
...<Bug>...
...<Bug>...
with
...<BugDTO>...
...<BugDTO>...
, and change the lambda expr in
_context.Bugs.Select(...)
_context.Bugs.Select(...)
to also convert the
Bug
Bug
s to
BugDTO
BugDTO
s, then the error in the screenshot happens.
I originally thought it was due to
isFixed
isFixed
being nullable, but forgiving null doesn't change anything. The error message also shows my lambda expr as
b => (bool?)BugsController...
b => (bool?)BugsController...
which could be a problem, but I don't know what. Both
allBugDTOList
allBugDTOList
and
filteredBugDTOList
filteredBugDTOList
are of type
List<BugDTO>
List<BugDTO>
as well, assuming I made the DTO-related changes in my 2nd paragraph.
Any help's appreciated, I'm still very new to C#
unknown.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ could not be translated
C#CC# / help
17mo ago
Await Linq Expression
C#CC# / help
4y ago
LINQ Lambda-Expression Diff
C#CC# / help
4y ago
✅ System.InvalidOperationException: The LINQ expression
C#CC# / help
2y ago