mountaindweller
mountaindweller

Reputation: 85

Linq selecting sub results using select

I'm not sure if I'm looking to use a union here or if this will work.

    var users =
        db.AspNetUsers.Where(
            x => x.AspNetRoles.Select(y => y.Id).Contains("4575754-799a-4bhe-8caf-00002344b7a6"))
            .Select(d => d.PatientId);

    var patients = from patient in db.Patients
        where patient.LastName == lastName && clinicId == patient.ClinicId && users.Contains(patient.Id)
        select new
        {
            patient.LastName,
            patient.AspNetUsers.First(d=>d.Email),
            patient.Sex
        };

This works when I get everything from patients, but when I try to get the email address from the AspNetUsers object I get the message 'can not convert expression type string to return type bool'

When I am in quickwatch, I can see the entire AspNetUsers object returned inside the patient object, but why can't I easily select a single property out of that list?

Upvotes: 2

Views: 128

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

This expression in LINQ

patient.AspNetUsers.First(d=>d.Email)

means "get me the first AspNetUser whose boolean attribute Email is true". Of course this makes no sense, because Email is a string, not a boolean.

What you need is

patient.AspNetUsers.First().Email

Upvotes: 1

Related Questions