读书人

LINQ to Entities 不支持带参数的构造

发布时间: 2011-12-12 22:59:56 作者: rapoo

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;

读书人网 >.NET

热点推荐