-1

I am using entity framework core in my repository get different model get/add/update/delete and getall Now for one scenario i need to right a custom query to get count. I am unable to found any method where i can run the custom query. Have any body used it.

Kamran Shahid
  • 3,616
  • 3
  • 39
  • 83
  • 1
    Does this answer your question? [Raw SQL queries and Entity Framework Core](https://stackoverflow.com/questions/35305825/raw-sql-queries-and-entity-framework-core) – Progman Oct 28 '20 at 09:52
  • no it doesn't answer – Kamran Shahid Oct 28 '20 at 19:12
  • Please [edit] your question to include the source code you have and the attempts you have tried. Also include the result/error messages from your attempts and the problems you have executing them. `context.Database.ExecuteSqlCommand()` looks promising. – Progman Oct 28 '20 at 19:36
  • Also see other questions like https://stackoverflow.com/questions/35631903/raw-sql-query-without-dbset-entity-framework-core – Progman Oct 28 '20 at 19:37

1 Answers1

0

Create Data Transfer Objects (DTOs) eg.

 public class yourclassname
    {
        public string yourfiledname { get; set; }
        public int count { get; set; }
    }

then you

public IActionResult gettop10staffcount()
    {
        var query = "your query";
        var result = Context.yourclassname.FromSql(query).ToList();
        return new ObjectResult(result);
    }
       

https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

Chameera
  • 395
  • 2
  • 8
  • it's a general query not specific to any particular table. what class name could i put? it will have join with multiple tables – Kamran Shahid Oct 28 '20 at 19:13