user606934
user606934

Reputation: 23

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.Amount)
  }).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!

Upvotes: 0

Views: 1998

Answers (1)

SLaks
SLaks

Reputation: 887195

Change your MyModel.date property from object to DateTime.

Alternatively, change your code to

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

Upvotes: 1

Related Questions