-2

I have a problem using async, and Task.

I have this function:

public async Task<List> Read(int id)
    {
        using (var db = new SmartFridgeContext())
        {
            var list = await db.Lists.Include(l => l.ListItems.Select(li => li.Item)).SingleOrDefaultAsync(l => l.ListId == id);
            return list;
        }
    }

Been trying for hours, but I don't know how to get a List from Task<List>. What I mean, is that I want the "pure" list back, so I can use it.

Can anyone help?

EDIT:

To clear up confusion, this is a List:

public class List
{
    public int ListId { get; set; }
    public string ListName { get; set; }

    public ICollection<ListItem> ListItems { get; set; } 

    public List(string listname)
    {
        ListName = listname;
    }
}

I know I will need to await the Task, but then I need to declare the function async. What if I want to call Read(int id) in Main()?

Thisen
  • 183
  • 1
  • 9

1 Answers1

0

You need to await the task.

Use it like this:

List myList = await Read(5);

If you want to use it in the Main method, the compiler won't let you await the method. Use this:

List myList = Read(5).Result;
mmking
  • 1,564
  • 4
  • 26
  • 36
  • What if I for an example wants to call that in Main()? Then the compiler tells me to make the method Async. – Thisen Apr 03 '15 at 19:16
  • @Thisen - that's an entirely different question, now you're asking how you can call async methods from main See http://stackoverflow.com/questions/9208921/async-on-main-method-of-console-app – Erik Funkenbusch Apr 03 '15 at 19:29
  • 1
    @Thisen `var x = Read(5).Result` – EZI Apr 03 '15 at 19:31
  • When I use the code you told me to, the Task.WaitAll(myTask) gives me an AggregateException. – Thisen Apr 03 '15 at 19:31
  • 1
    @Thisen - also see http://blog.stephencleary.com/2012/02/async-console-programs.html – Erik Funkenbusch Apr 03 '15 at 19:33
  • I'm probably confused about something, but `List myList = Read(5).Result;` gives me a AggregateException too. – Thisen Apr 03 '15 at 19:48
  • @Thisen My guess is that there's something wrong with the Read method. Maybe it was cancelled or something. – mmking Apr 03 '15 at 19:57
  • @Thisen then put a break-point and see the inner exceptions. How can we help if you get an exception in your Read method which is not clear for us what it is doing. – EZI Apr 03 '15 at 19:58
  • @Thisen - an "AggregateException" means that the real exception is in the InnerException (or maybe even the InnerException of another InnerException) so you have to look deeper. – Erik Funkenbusch Apr 03 '15 at 20:15
  • @EZI Just made this method (for test): `public static Task Reader(int id) { var manage = new ListManager(); return manage.Read(id); }` Which gives me no exceptions, so it seems like there's nothing with th Read method. – Thisen Apr 03 '15 at 20:25
  • @Thisen OK, I say your original method returns expections not your test case. If you insist to say "a correct method returns exception", good to learn a new thing every day. – EZI Apr 03 '15 at 20:49