1

I have a function named GetParentViewModel as shown below, that converts a Parent model class to a ParentViewModel so that it can be sent to a view.

   public class Parent
   {
        public int ParentId { get; set; }
        public string ParentName { get; set; }
        public IEnumerable<Child> children{ get; set; }
   }

   public class Child
   {
        public int ChildId { get; set; }
        public string ChildName { get; set; }
        private IEnumerable<Friend> friends { get; set; }
        public IEnumerable<Hobby> hobbies { get; set; }
   }

   public async Task<List<Hobby> GetHobbies(int childId)
   {
       return await..
   }

   public async Task<List<Friend> GetFriends(int childId)
   {
       reutrn await..
   }

   public async Task<ParentViewModel> GetParentViewModel(Parent parentModel)
   {
      return new ParentViewModel() 
      {
         ParentId = parentModel.ParentId,
         ParentName = parentModel.ParentName,
         children = parentModel.children.Select(item => new ChildViewModel()
         {
             ChildId = item.ChildId,
             ChildName = item.ChildName,
             hobbies = await GetHobbies(item.ChildId),
            friends = await GetFriends(item.ChildId)
          }); 
       };
   }

when I try to call the async functions to GetHobbies and GetFriends, within GetParentViewModel, I get an error

   CS4034: The ‘await’ operator can only be used within an async lambda expression. Consider marking this lambda expression with the async modifier.

I have tried all sorts of things, tried marking as async just before "Select( item=>.." no success, had an await keyword just after "children = await parentModel" but it doesn't seem to work.. could somebody help..

I got it working by writing a separate async function to get the children but not way above. Any suggestions please

thank you.

Milen
  • 8,697
  • 7
  • 43
  • 57
Pravi
  • 77
  • 1
  • 8
  • 2
    Possible duplicate of [Where do I mark a lambda expression async?](https://stackoverflow.com/questions/14015319/where-do-i-mark-a-lambda-expression-async) – Rawling Apr 25 '19 at 12:54
  • Sorry made a small change to show that code returns ParentViewModel.. cheers – Pravi Apr 25 '19 at 12:56

1 Answers1

2

Update: I would recommend refactoring to something like this:

    public async Task<ParentViewModel> GetParentViewModel(Parent parentModel)
    {
        var childrens = new List<ChildViewModel>();
        foreach (var item in parentModel.children)
        {
            var hobbies = await GetHobbies(item.ChildId);
            var friends = await GetFriends(item.ChildId);

            var child = new ChildViewModel
            {
                ChildId = item.ChildId,
                ChildName = item.ChildName,
                hobbies = hobbies,
                friends = friends
            };

            childrens.Add(child);
        }

        return new ParentViewModel
        {
            ParentId = parentModel.ParentId,
            ParentName = parentModel.ParentName,
            children = childrens
        };
    }
Milen
  • 8,697
  • 7
  • 43
  • 57
  • thank you, but I get this error - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable>' to 'System.Collections.Generic.IEnumerable'. An explicit conversion exists (are you missing a cast?) – Pravi Apr 25 '19 at 13:05
  • thank you.. Yes, cheers, I think, I have done that and got this working, but wondered how to get that working the way I had shown in code above.. – Pravi Apr 25 '19 at 13:38
  • See this SO question as well: https://stackoverflow.com/questions/13046096/can-not-await-async-lambda – Milen Apr 25 '19 at 13:40