bragin.www
bragin.www

Reputation: 199

linq to sql Cannot implicitly convert type... error

I always catch error in getting strongly typed value from my query. I need to get DateTime variable from such query

        var lastdateindex = (from c in v.db.TotalDoc
                             select c).Max(id => id.TotalID);
        var lastdate = (from c in v.db.TotalDoc
                        where c.TotalID == lastdateindex
                        select c.TotalDate);
        List<DateTime> list = lastdate.ToList();

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

i don't know what should i do. please help. thank you

Upvotes: 0

Views: 1087

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503629

The problem is that your TotalDate type is DateTime?, not DateTime. Now if you're sure that all rows will have a value, you could use:

var lastDateIndex = v.db.TotalDoc.Max(id => id.TotalID);
var lastDate = from c in v.db.TotalDoc
               where c.TotalID == lastDateIndex
               select c.TotalDate.Value;
List<DateTime> list = lastDate.ToList();

Or you might want to only pick those rows with a date:

var lastDateIndex = v.db.TotalDoc.Max(id => id.TotalID);
var lastDate = from c in v.db.TotalDoc
               where c.TotalID == lastDateIndex && c.TotalDate != null
               select c.TotalDate.Value;
List<DateTime> list = lastDate.ToList();

Upvotes: 2

Related Questions