0

I am trying to work out why is the first block of code erroring out and the second block isnt when the only difference is that the first one is using Task and the second block isnt. Looked into various sources but couldnt get clear answer. Anyone knows how to fix the first block?

referred to links below but no luck so far How to cast DbSet<T> to List<T>
Why DbSet<TEntity> doesn't implement EnumerableAsync

    public Task<IEnumerable<Asset>> GetAll()
    {
        var assets = Task.Factory.StartNew(() => _context.Books);
        return assets;
       // tried  toList<asset>() on "return assets" as well but didnt make any difference.
    }


    public IEnumerable<Asset> GetAlls()
    {
        var assets = _context.Books;
        return assets;
    }
Community
  • 1
  • 1
Sike12
  • 1,232
  • 6
  • 28
  • 53
  • Why do you return _context.Books in a Task? What's a point? It does nothing useful it seems. – Evk Apr 09 '16 at 18:52
  • 1
    You can just cast _context.Books to (IEnumerable) inside StartNew to solve your immediate problem, but question is why you are doing this in the first place. – Evk Apr 09 '16 at 18:55
  • @Evk i am just familiarising with Task at the moment. its just an example. i will take your feedback. – Sike12 Apr 09 '16 at 18:56
  • @Evk you cracked it. Thank you very much. Please Post it as answer and i'll accept it. Also any suggestions on making this code better would be highly appreciated in your answer. – Sike12 Apr 09 '16 at 18:59

3 Answers3

1

You basically just need this:

public async Task<IEnumerable<Asset>> GetAllAsync()
{
    return await _context.Books;
}

The Books of _context would be a IEnumerable<Asset>. Using the async/await you basically achieve to load the Books asynchronously. The basic difference between the above method and the following method

public IEnumerable<Asset> GetAlls()
{
    var assets = _context.Books;
    return assets;
}

is that when you will request to iterate through the result of GetAlls or you create a list based on this result by calling the ToList method, you will make a synchronous call (a blocking call, the current execution would be freeze until you get the results you requested. While in the first case you release the thread in which the GetAllAsync() is called and when the results would be available the execution of your code would continue on another thread or in the thread that GetAllAsync() is called provided that the data are available the moment that are requested)

Christos
  • 53,228
  • 8
  • 76
  • 108
1

I know this is old thread. In 2018 this code helps me (probably, it helps someone else):

public async Task<IEnumerable<Asset>> GetAll()
{
    var assets = _context.Books.AsEnumerable();
    return assets;
}
Barabas
  • 912
  • 8
  • 19
0

To fix, just make your Task return the expected type, that is IEnumerable<Asset>:

public Task<IEnumerable<Asset>> GetAll()
{
    var assets = Task.Factory.StartNew(() => (IEnumerable<Asset>) _context.Books);
    return assets;
}


public IEnumerable<Asset> GetAlls()
{
    var assets = _context.Books;
    return assets;
}

But be aware that it doesn't make any sense, and is not the correct way of using Task and async/await.

Mohammad Dehghan
  • 17,853
  • 3
  • 55
  • 72
Evk
  • 98,527
  • 8
  • 141
  • 191
  • Thank you very much. Any suggestions to improve this code would be highly welcomed as you mentioned that i shouldnt be doing this in the first place. – Sike12 Apr 09 '16 at 19:02
  • 1
    If you want to load Books asynchronously, just do _context.Books.ToListAsync() (where ToListAsync() is extension method from EF 6). – Evk Apr 09 '16 at 19:07
  • 2
    If you are familiriazing yourself with tasks, you also have to realize that in your code, task starts, immediatly returns _context.Books (it does NOT makes any queries or anything at all, just returns _context.Books as is) and exits. So it makes no useful work at all. – Evk Apr 09 '16 at 19:09
  • I completely agree on that @Evk. Thank you – Sike12 Apr 09 '16 at 20:50
  • Using `Task.Factory.StartNew` queues the operation to run on a the Thread Pool. It unnecessarily consumes an additional thread. – Mohammad Dehghan Dec 07 '18 at 20:25
  • @MohammadDehghan no doubt, I just copied code from OP post. Code does not make any sense as is anyway, which I explain in comments. – Evk Dec 07 '18 at 20:30