3

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

    public JsonResult GetChartData_IncidentsBySiteStatus(string SiteTypeId, string searchTextSite, string StartDate, string EndDate)
    {
        if (searchTextSite == null)
            searchTextSite = "";

        DateTime startDate = DateTime.Parse(StartDate);
        DateTime endDate = DateTime.Parse(EndDate);

        var sitesQry = _db.Sites;
        if (SiteTypeId != "-1")
            sitesQry = sitesQry.Where(a => a.SiteTypeId.ToString() == SiteTypeId);

        var qry = from i in _db.Incidents   
                  join s in _db.Sites on i.SiteId equals s.SiteId
                  where s.SiteDescription.Contains(searchTextSite)
                    && (i.Entered >= startDate && i.Entered <= endDate)
                  group s by s.SiteStatus.SiteStatusDescription + "[" + s.SiteTypeId.ToString() + "]"
                      into grp
                      select new
                      {
                          Site = grp.Key,
                          Count = grp.Count()
                      };

        return Json(qry.ToList()  , JsonRequestBehavior.AllowGet);
    }

...........the error is:

Error 7 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Linq.Table'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\Administrator\Desktop\IRenewables_EMAS\IRenewables_EMAS\Controllers\IncidentController.cs 69 28 Emas.Web

Can anyone suggest a workaround for this?

thanks

4
  • Which line exactly is line 11? The casting is incorrect, and that particular IQueryable object is not providing an Table interface. Commented Jul 11, 2011 at 8:21
  • Line 11: sitesQry = sitesQry.Where(a => a.SiteTypeId.ToString() == SiteTypeId); Commented Jul 11, 2011 at 8:52
  • why not get rid of that line and in your query go : where (s.SiteId.Equals(SiteTypeId) && !s.SiteId.Equals("-1") ... Commented Jul 11, 2011 at 9:12
  • Alternatively you could potentially go var sitesQry = _db.Sites.AsQueryable(); ... Commented Jul 11, 2011 at 9:20

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

While you suggestion is correct, I feel as though your explanation is not. IQueryable is not a base class, its an interface. This means that, while the Table class may implement the interface, calling it a base/sub class relationship is fundamentally incorrect.
0

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();

Comments

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.