1

Long story short, we have a C# WebApi that provides mapped instances from our DAL into DTOS.

Each controller exposes a IEnumerable<T> Get()

I have a test that finds all the controllers, and call this get method.

This flushes out any mapping errors that only occur on execution.

A while I noticed that to be sure you had to actually execute the enumeration otherwise the the mapping never takes place.

So I have this code

if (results is IEnumerable enumerable)
{
    var counter = enumerable.Cast<object>().Count();
    Trace.Write($"{counter} results found.");
}

This executes the cast (which invokes the mapping) and gives me a count for output reasons.

This worked until the result set got massive - it nows just times out.

Most of my IEnumerables are actually IQueryable underneath

I need a way to Take(10) on an IQueryable that can work on reflected data/executed types. This will let the mapping work but on a smaller data set.

Help!

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
Jon H
  • 1,061
  • 4
  • 13
  • 32
  • 1
    You can use `Take` after `Cast`: `Cast().Take(10).Count()` - only the first 10 objects will be counted. Of course, the `Count()` will always be no more than 10. – NetMage Nov 09 '18 at 21:01

0 Answers0