ยฉ 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#โ€ข2y agoโ€ข
32 replies
Cyclomatic

Entity Framework IQueryable question

I'm trying to get a list of prescriptions from my Patients entity:

        var kk = await _context.Patients.Where(p => p.Id == id)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Medicine)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Doctor)
            .ToListAsync();

        var gg = await _context.Patients.Where(p => p.Id == id)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Medicine)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Doctor)
            .Select(p => new
            {
                Prescription = p.Prescriptions.Select(p => p.ToDto())
            })
            .ToListAsync();
        var kk = await _context.Patients.Where(p => p.Id == id)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Medicine)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Doctor)
            .ToListAsync();

        var gg = await _context.Patients.Where(p => p.Id == id)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Medicine)
            .Include(p => p.Prescriptions)
            .ThenInclude(p => p.Doctor)
            .Select(p => new
            {
                Prescription = p.Prescriptions.Select(p => p.ToDto())
            })
            .ToListAsync();


kk works, but gg returns an empty list;. Any ideas why this might be happening? I know it's a short snippet, but I figured there would be something obvious I am doing wrong...

Thanks!
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

โœ… LINQ help: IQueryable<Entity> -> Dictionary<string, Entity[]>
C#CC# / help
12mo ago
Entity framework
C#CC# / help
14mo ago
โ” Entity Framework
C#CC# / help
3y ago
โ” Entity Framework
C#CC# / help
4y ago