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();
精彩评论