简体   繁体   中英

C# LINQ EntityFramework trouble

I have a property that executes a LINQ query. Why does it return a bool? How can I make it return an instance of my ORMClass?

public string ContactPersonName
{
    get
    {
        return Convert.ToString(
            Client.ContactPersons.Select(x => x.MainContactPerson == true).First()
        );
    }
}

I want some of

((ContactPerson)Client.ContactPersons.Select(x => x.MainContactPerson == true).First())).Name //typecast error

You should use Where to filter instead of Select

Client.ContactPersons.Where(x => x.MainContactPerson).First();

For simpler:

Client.ContactPersons.First(x => x.MainContactPerson);

You're doing a Select when you really want a Where

public string ContactPersonName
{
    get
    {
        return Convert.ToString(
            (
                Client.ContactPersons.Where(x => x.MainContactPerson == true).First())
            )
            ;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM