-2
public enum SyncListsEnum
{
    Country = 1,
    Language = 2
}

public class SyncListsService
{
    private readonly WeCandidateContext _db;
    private readonly IRestClientFactory _factory;
    private readonly IConfigurationService _configService;

    private readonly Dictionary<SyncListsEnum, Action<IList<ExpandoObject>>> _syncLists;

    public SyncListsService(WeCandidateContext db, IRestClientFactory factory, IConfigurationService configService)
    {
        _db = db;
        _factory = factory;
        _configService = configService;

        _syncLists = new Dictionary<SyncListsEnum, Action<IList<ExpandoObject>>>()
        {
            {SyncListsEnum.Country, items =>{ new CountrySync(_db).SyncItems(items); _db.SaveChangesAsync();}},
            {SyncListsEnum.Language, items =>{ new LanguageSync(_db).SyncItems(items); _db.SaveChangesAsync();}}
        };
    }

    public async Task SyncList(string listName)
    {
        SyncListsEnum list;
        if (!Enum.TryParse(listName, true, out list)) return;


        // get list content from Recruiter 
        var items = await GetListFromRecruiter();
        // call a specific routine for each list to handle insert/update/delete 
        if (items != null)
        {
            // Synchronizes the local table with items retrieced from Recruiter.
            this._syncLists[list](items);
        }
    }
}

In this code all are correct but i need await _db.SaveChangesAsync() instead of _db.SaveChangesAsync(). If i put await operator in front of _db.SaveChangesAsync(), it shows me an error like await operator can only be used within an async lamda expression.

Now where should i use async keyword to resolved this error.

Arif
  • 6,094
  • 4
  • 49
  • 81
  • I guess it would be better to return the `Task` without adding `await` in the delegate –  Feb 27 '17 at 17:11

1 Answers1

1

Put async in front of your lambda expressions, then you can use await. See "can not await async lambda"

Community
  • 1
  • 1
adam0101
  • 29,096
  • 21
  • 96
  • 174