开发者

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

开发者 https://www.devze.com 2023-02-08 21:21 出处:网络
I\'ve tried numerous things to fix what seems to be a simple problem.Any help is greatly appreciated!

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

精彩评论

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