Search code examples
mysqlentity-frameworkasp.net-core-2.0pomelo-entityframeworkcore-mysql

Selecting specific row from database .net core 2


I want to retrieve a specific row from the database. so I'm using this command to retrieve it :

 UserOwner userowner =_context.User.FromSql("SELECT * FROM db.user WHERE name = 'username'").FirstOrDefault();
                return Ok(user);

the thing is this returns a list and I return the first on the list, but isn't there a way to return straight from the SQL command one row?


Solution

  • try use labda function on FirstOrDefault.

    UserOwner userowner =_context.User.FirstOrDefault(o=> o.name == "username" );
                    return Ok(user);
    

    but if you need use an SQL script, for MYSQL use:

    "SELECT * FROM db.user WHERE name = 'username' LIMIT 1"