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.