简体   繁体   中英

How can I use a where clause to select?

Client table has columns Id, Name, Value .

I'm trying to use a .where clause to return the records where name column has a value of CLIENTACCESS

ClientAccountAccess clientAccessCodes = db.ClientAccountAccesses
                    .Where(x => x.name == "CLIENTACCESS").Select();

return clientAccessCodes.value;

I don't think I'm too far off....any help?

You can use FirstOrDefault , if there is no record that mathces your condition then it will return null.

ClientAccountAccess clientAccessCodes = db.ClientAccountAccesses
                                      .Where(x => x.name == "CLIENTACCESS")
                                      .FirstOrDefault();
if(clientAccessCodes != null)
    return clientAccessCodes.value;
else
   return null;

Also you can use this overloaded version of FirstOrDefault and you can shorten that statement like this:

var clientAccessCodes = db.ClientAccountAccesses
                          .FirstOrDefault(x => x.name == "CLIENTACCESS");

You don't need Select() . Just do this:

return db.ClientAccountAccesses.FirstOrDefault(x => x.name == "CLIENTACCESS");

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