开发者

Unable to cast the type 'System.DateTime' to type 'System.Object'. LINQ

I've tried numerous things to fix what seems to be a simple problem. Any help is greatly appreciated!

Code:

Class:

public class MyModel
{
    public DateTime date { get; set; }
    public int total { get; set; }
}

List<MyModel> query = (from ds in dataSource
  group ds by ds.someDate into dsg
  select new MyModel
  {
    date = dsg.Key,
    total = dsg.Sum(ds => ds.Amo开发者_高级运维unt)
  }).ToList<MyModel>();

Error:

Unable to cast the type 'System.DateTime' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model primitive types.

Thanks!


Change your MyModel.date property from object to DateTime.

Alternatively, change your code to

List<DateTime> dates = dataSource.Select(ds => ds.someDate)
                                 .Distinct()
                                 .ToList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜