c#在控制器中使用LINQ语法生成错误 [英] c# Build Error with LINQ syntax in Controller

查看:35
本文介绍了c#在控制器中使用LINQ语法生成错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此控制器代码的第11行出现以下错误:

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:

错误7无法隐式转换类型 'System.Linq.IQueryable'到 'System.Data.Linq.Table'.显式转换 存在(您是否缺少演员表?)C:\ Documents and 设置\管理员\桌面\ IRenewables_EMAS \ IRenewables_EMAS \ Controllers \ IncidentController.cs 69 28 Emas.Web

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?

谢谢

推荐答案

您正试图分配一个子类实例.

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

代替

var sitesQry = _db.Sites;

尝试使用

IQueryable<Site> sitesQry = _db.Sites;

这篇关于c#在控制器中使用LINQ语法生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆