Async lambda expression not compiling

I have a lambda expression with a Select where a new ResourcesResponse.Resource object is created. The Resource object has a calculated Order field which calls an async repository method called GetResourceTypeOrder and everything looks as follows:

ResourcesResponse instantiation:

var tmp = new ResourcesResponse
{
    Cost = aux.Sum(s => s.Cost),
    Resources = aux
        .OrderBy(k => k.Order)
        .ThenBy(k => k.TypeId)
        .Select(async s => new ResourcesResponse.Resource
        {
            Id = ((ulong)s.Id).ToString(),
            Charge = s.Charge,
            Cost = s.Cost,
            CurrencyCode = s.CurrencyCode,
            ForecastCharge = s.ForecastCharge,
            ForecastCost = s.ForecastCost,
            ForecastUsedUnits = s.ForecastUsedUnits,
            ParentId = ((ulong?)s.ParentId).ToString(),
            Order = await _resourcesRepository.GetResourceTypeOrder(s.TypeId),
            Rate = s.Rate,
            TypeId = s.TypeId,
            UsedUnits = s.UsedUnits,
        })
        .ToArray(),
    Id = ((ulong)entry.Id).ToString(),
    Name = entry.Name,
    Description = entry.Description,
    OriginalId = entry.OriginalId,
    ResourceTypeId = entry.ResourceTypeId,
    ResourceType = await _resourcesRepository.GetResourceTypeName(entry.ResourceTypeId),
    Order = await _resourcesRepository.GetResourceTypeOrder(entry.ResourceTypeId),
    State = entry.State,
    Zone = entry.Zone,
    Charge = aux.Sum(s => s.Charge),
};

ResourceResponse:

public class ResourcesResponse
{
    public string Id { get; set; } = default!;
    public string Name { get; set; } = default!;
    public string? Description { get; set; } = default!;
    public string OriginalId { get; set; } = default!;
    public string ResourceType { get; set; } = default!;
    [JsonIgnore]
    public int? Order { get; set; } = default!;
    public ResourceTypeValues ResourceTypeId { get; set; }
    public string? CurrencyCode => Resources?.FirstOrDefault()?.CurrencyCode;
    public decimal? Charge { get; set; }
    public decimal Cost { get; set; }
    public decimal? ForecastCharge { get; set; }
    public decimal? ForecastCost { get; set; }
    public string Zone { get; set; } = default!;
    public string State { get; set; } = default!;
    public Resource[] Resources { get; set; } = default!;

    public sealed class Resource
    {
        [JsonIgnore]
        public string Id { get; set; } = default!;
        [JsonIgnore]
        public string? ParentId { get; set; } = default!;
        [JsonIgnore]
        public int? Order { get; set; } = default!;
        public string CurrencyCode { get; set; } = default!;
        public decimal Rate { get; set; }
        public long UsedUnits { get; set; }
        public decimal Cost { get; set; }
        public decimal? Charge { get; set; }
        public long? ForecastUsedUnits { get; set; }
        public decimal? ForecastCost { get; set; }
        public decimal? ForecastCharge { get; set; }
        public string Type { get; set; } = default!;
        public ResourceTypeValues TypeId { get; set; }
        public string? Unit { get; set; } = default!;
        public bool? Hourly { get; set; } = default!;
    }
}

ResourceTypeOrder:

public async Task<int?> GetResourceTypeOrder(ResourceTypeValues resourceTypeId)
{
    ICache.Operation<int?> myTableCacheImplementation = async delegate (string key)
    {
        var dic = await _repositoryResourceTypes.FindAll().ToDictionaryAsync(k => k.Id);
        return dic[resourceTypeId].Order;
    };

    return await _vdcCache.GetOrCreateAsync(ResourceTypeCacheKey.ResourceTypeOrder, myTableCacheImplementation, null);
}

My problem is I don’t know where to put the missing await (or whatever is needed for the entire expression to be accepted by the compiler).

For the parent ResourceResponse there is also a calculated Order field, but as you can see that one is correct.

It is obvious that I could add a .Result to the end of GetResourceTypeOrder and remove the async operator, but that’s not the idea of an async / await.

8

I’ll answer myself:

The way to handle this (taking @Fildor comment as a start point) was to refactor the GetResourceTypeOrder method so it returns the complete dictionary, instead of a particular element. Then I just have to retrieve the dictionary before the ResourcesResponse instance creation and get -and assign- the corresponding Order to “Order” field as resourceTypes[s.TypeId].Order:

var resourceTypes = await _resourcesRepository.GetResourceTypes();

var tmp = new ResourcesResponse
{
    Cost = aux.Sum(s => s.Cost),
    Resources = aux
        .OrderBy(k => k.Order)
        .ThenBy(k => k.TypeId)
        .Select(s => new ResourcesResponse.Resource
        {
            Id = ((ulong)s.Id).ToString(),
            Charge = s.Charge,
            Cost = s.Cost,
            CurrencyCode = s.CurrencyCode,
            ForecastCharge = s.ForecastCharge,
            ForecastCost = s.ForecastCost,
            ForecastUsedUnits = s.ForecastUsedUnits,
            ParentId = ((ulong?)s.ParentId).ToString(),
            Order = resourceTypes[s.TypeId].Order,
            Rate = s.Rate,
            TypeId = s.TypeId,
            UsedUnits = s.UsedUnits,
        }).ToArray(),
    Id = ((ulong)entry.Id).ToString(),
    Name = entry.Name,
    Description = entry.Description,
    OriginalId = entry.OriginalId,
    ResourceTypeId = entry.ResourceTypeId,
    ResourceType = await _resourcesRepository.GetResourceTypeName(entry.ResourceTypeId),
    Order = await _resourcesRepository.GetResourceTypeOrder(entry.ResourceTypeId),
    State = entry.State,
    Zone = entry.Zone,
    Charge = aux.Sum(s => s.Charge),
};

GetResourceTypes:

public async Task<IReadOnlyDictionary<ResourceTypeValues, ResourceTypes>> GetResourceTypes()
{
    ICache.Operation<IReadOnlyDictionary<ResourceTypeValues, ResourceTypes>> myTableCacheImplementation = async delegate (string key)
    {
        return await _repositoryResourceTypes.FindAll().ToDictionaryAsync(k => k.Id);
    };

    return await _vdcCache.GetOrCreateAsync(ResourceTypeCacheKey.ResourceTypeOrder, myTableCacheImplementation, null);
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật