0

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??

Community
  • 1
  • 1
Faust
  • 15,130
  • 9
  • 54
  • 111

1 Answers1

2

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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • .Where(x => x.HasValue) was the ticket. Note though: I still needed AsEnumerable(); – Faust May 23 '11 at 10:07
  • @Faust: If `GetPubsDistinctMonthYears` returns `ObjectResult` you shouldn't need `AsEnumerable`, as that implements `IEnumerable`. What was going wrong when you didn't call `AsEnumerable`? – Jon Skeet May 23 '11 at 10:14