c# Build Error with LINQ syntax in Controller

后端 未结 2 1938
独厮守ぢ
独厮守ぢ 2021-01-21 08:44

Hi folks I have the following error on line 11 in this controller code :

    public JsonResult GetChartData_IncidentsBySiteStatus(string SiteTypeId, string searc         


        
相关标签:
2条回答
  • 2021-01-21 09:04

    You are trying to assign a base class instance to a sub class instance.

    Instead of

    var sitesQry = _db.Sites;
    

    try using

    IQueryable<Site> sitesQry = _db.Sites;
    
    0 讨论(0)
  • 2021-01-21 09:20

    While @Eranga 's solution may work, I feel as though there are more options and a better explanation available.

    Firstly, the reason that you cannot assign the value is because you are trying to assign and implementation of an interface to a concrete type. THis cannot be done because you have no idea what could be implementing that interface, and this means that the it cannot be assigned to something that has a concrete type.

    There are multiple options for this. You can do as @Eranga suggests and create a new variable

    IQueryable<Site> sitesQry = _db.Sites;
    

    or you can call the .CopyToDataTable extension method that is exposed by Linq.

    sitesQry = _db.Sites.CopyToDataTable();
    
    0 讨论(0)
提交回复
热议问题