LINQ to SQL语法需要帮助

时间:2011-07-09 12:59:24

标签: c# linq-to-sql

嗨,我正在混淆尝试按LINQ中的结果集构建一个组

以下TSQL在LINQ中得到我想要的,表达式是:

select s.SiteDescription,count(*) as TotalIncidents
from Site as s,Incident as i
where s.SiteId = i.SiteId
group by s.SiteDescription

但是我想在我的控制器代码中在LINQ中创建相同的东西,这是我所拥有的,但它没有好处,我今天到处都是:

var qry = from s in _db.Sites
            join i in _db.Incidents on s.SiteId equals i.SiteId
            group s.SiteDescription
                into grp
                select new
                {
                    Site = grp.Key,
                    Count = grp.Select(x => x.Incidents).Distinct().Count()
                };

我的错误是:

  

错误27
  无法将lambda表达式转换为类型
  'System.Collections.Generic.IEqualityComparer'   因为它不是代表   键入C:\ Documents and   设置\管理员\桌面\ IRenewables_EMAS \ IRenewables_EMAS \ Controllers \ IncidentController.cs 40 23 Emas.Web
  错误28
  方法的类型参数   “System.Linq.Enumerable.OrderBy(System.Collections.Generic.IEnumerable,   System.Func)'不能   从用法推断。尝试   指定类型参数   明确。 C:\ Documents and   设置\管理员\桌面\ IRenewables_EMAS \ IRenewables_EMAS \ Controllers \ IncidentController.cs 51 33 Emas.Web
  错误29
  预期的上下文关键字'by'C:\ Documents and   Settings \ Administrator \ Desktop \ IRenewables_EMAS \ IRenewables_EMAS \ Controllers \ IncidentController.cs 41 27 Emas.Web

先谢谢,

1 个答案:

答案 0 :(得分:2)

试试这个:

var qry =   from s in _db.Sites
            join i in _db.Incidents on s.SiteId equals i.SiteId
            group s by s.SiteDescription into grp
            select new
            {
                Site = grp.Key,
                Count =  grp.Count()
            };