Faust
Faust

Reputation: 15404

ObjectResult<Nullable<DateTime>> to IEnumerable<DateTime>?

I've attached a stored procedure (see my previous question w/proc code) to my Entity Framework model with function import, selecting "Scalars: DateTime" to get a collection of DateTimes as the return type. I am calling that in my repository:

return _DataContext.GetPubsDistinctMonthYears(pubType, pubId).AsEnumerable();

I need the repository method to return IEnumerable; however, the function is returning ObjectResult<Nullable<DateTime> (with the correct DateTime values in it), and if I cast it as IEnumerable, the result is just null.

"Safe cast" doesn't work either:

return _DataContext.GetPubsDistinctMonthYears(pubType, pubId) as IEnumerable<DateTIme>;

QUESTION
So what do I need to do either/both in the stored procedure and the repository to get my IEnumerable??

Upvotes: 0

Views: 1470

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502526

It looks like you just want something like:

return _DataContext.GetPubsDistinctMonthYears(pubType, pubId)
                   .Select(x => x.Value);

That will go bang if there are any null values though. To ignore null values, you could use:

return _DataContext.GetPubsDistinctMonthYears(pubType, pubId)
                   .Where(x => x.HasValue)
                   .Select(x => x.Value);

Upvotes: 2

Related Questions