日期:2014-05-20  浏览次数:21003 次

LINQ to Entities 不支持带参数的构造函数该怎么办?
C# code

return from p in context.oxite_Post
                    where p.oxite_Area.Any(a => a.oxite_Site.SiteID == siteID) && p.PublishedDate <= DateTime.UtcNow && p.State == (byte)EntityState.Normal
                    let month = [color=#FF0000]new DateTime[/color](p.PublishedDate.Year, p.PublishedDate.Month, 1)
                    group p by month into months
                    orderby months.Key descending
                    select [color=#FF0000]new KeyValuePair[/color]<ArchiveData, int>(new ArchiveData(months.Key.Year + "/" + months.Key.Month), months.Count());




程序运行出错:LINQ to Entities 仅支持无参数构造函数和初始值设定项。

这可怎么办?在linq to sql 里是没有问题的。。。

------解决方案--------------------
你可以换一种思路:

var query=from p in context.oxite_Post.ToList()
where p.oxite_Area.Any(a => a.oxite_Site.SiteID == siteID) && p.PublishedDate <= DateTime.UtcNow && p.State == (byte)EntityState.Normal
let month = Convert.ToDateTime(p.PublishedDate.Year+"-"+p.PublishedDate.Month+"-"+1)
group p by month into months
orderby months.Key descending
select new

AH=new ArchiveData{Name=months.Key.Year + "/" + months.Key.Month}, 
CT=months.Count()
};

 var result=new List<KeyValuePair<ArchiveData, int>>();
 query.ForEch(q=> result.Add(new KeyValuePair<ArchiveData, int>(q.AH,q.CT));

return result;