0

If i have a table as called "users" and i want give only a query for example to show user as have id > 1.
How to do that in ASP.NET Core.
in php you can do like that:
$sql = "SELECT * FROM users WHERE id='1';";
but how to do same with ASP.NET CORE.
Sorry for my bad English.

1 Answer 1

1

You can use the FromSqlRaw extension method to begin a LINQ query based on a raw SQL query. FromSqlRaw can only be used on query roots, that is directly on the DbSet<>.

Note:DbSet.FromSql prior to Entity Framework Core 3.0

For parameterized queries , you could refer to the below examples:

1)Format string

 var author = db.Authors.FromSqlRaw("SELECT * From Authors Where AuthorId = {0}", id).FirstOrDefault();

2)Create DbParameter objects for the provider that you are using.

var p1 = new SqliteParameter("@Id", id);   // parameter construction for SqLite
var author = db.Authors.FromSqlRaw($"SELECT * From Authors Where AuthorId = @Id", p1).FirstOrDefault();

Reference:

https://learn.microsoft.com/en-us/ef/core/querying/raw-sql

https://www.learnentityframeworkcore.com/raw-sql

Sign up to request clarification or add additional context in comments.

1 Comment

I will try it and come back. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.