Home > OS >  Convert method to async
Convert method to async

Time:06-18

I'm trying to make this method asynchronous.

I found posts related to my question ( you might think this is duplicate) and I'm not sure how to apply them to this method.

Could use some help.

public async Task<IEnumerable<E2307DetailsViewModel>> GetE2307Details(long headerID)
    {
        // it's slow here; takes a lot of steps when finding the header Id
        var E2307Details = await entities.AP_SUPPLIER_2307.AsEnumerable().Where(x => x.AP_2307_HDR_ID == headerID).Select(x => new E2307DetailsViewModel
        {
            APSupplier2307ID = x.AP_SUPPLIER_2307_ID,
            AP2307HeaderID = x.AP_2307_HDR_ID,
            UploadFileID = x.UL_ID,
            TransactionAPJEID = x.TRANS_APJE_ID,
            TransactionDescription = x.TRANS_DESCRIPTION,
            TransactionDate = x.TRANS_DATE,
            ReferenceNo = x.REFERENCE_NO,
            InvoiceNo = x.INVOICE_NO,
            ATCCode = x.ATC_CODE,
            TaxRate = x.TAX_RATE,
            AmtOfTaxWithHeld = x.AMOUNT_OF_TAX_WITHHELD,
            ForTheMonthOf = GetUploadFileDetails(x.UL_ID).FOR_THE_MONTH_OF,
            IncomePayment = x.AMOUNT_OF_TAX_WITHHELD / (x.TAX_RATE / 100),
            MonthNo = GetUploadFileDetails(x.UL_ID).FOR_THE_MONTH_OF_NO.GetValueOrDefault(0),
            NatureOfPayment = GetTaxCode().FirstOrDefault(y => y.ATCCode == x.ATC_CODE).NatureOfPayment,
            ForTheYearOf = GetUploadFileDetails(x.UL_ID).FOR_THE_YEAR
        });

        return E2307Details;
    }

Edit: I tried replacing 'Where' with 'FirstOrDefaultAsync' but it says

IEnumerable<AP_Supplier_2307> does not contain a definition for 'FirstOrDefaultAsync'

Edit: When I checked this on Debug mode and tried "Step Into", it takes too much time in this code Where(x => x.AP_2307_HDR_ID == headerID) hence why I'm trying to make this method async.

CodePudding user response:

You may need to rewrite the method like this:

public async Task<IEnumerable<E2307DetailsViewModel>> GetE2307Details(long headerID)
    {
        // it's slow here; takes a lot of steps when finding the header Id
        var E2307Details = (await entities.AP_SUPPLIER_2307.Where(x => x.AP_2307_HDR_ID == headerID).ToListAsync())
        .Select(x => new E2307DetailsViewModel
        {
            APSupplier2307ID = x.AP_SUPPLIER_2307_ID,
            AP2307HeaderID = x.AP_2307_HDR_ID,
            UploadFileID = x.UL_ID,
            TransactionAPJEID = x.TRANS_APJE_ID,
            TransactionDescription = x.TRANS_DESCRIPTION,
            TransactionDate = x.TRANS_DATE,
            ReferenceNo = x.REFERENCE_NO,
            InvoiceNo = x.INVOICE_NO,
            ATCCode = x.ATC_CODE,
            TaxRate = x.TAX_RATE,
            AmtOfTaxWithHeld = x.AMOUNT_OF_TAX_WITHHELD,
            ForTheMonthOf = GetUploadFileDetails(x.UL_ID).FOR_THE_MONTH_OF,
            IncomePayment = x.AMOUNT_OF_TAX_WITHHELD / (x.TAX_RATE / 100),
            MonthNo = GetUploadFileDetails(x.UL_ID).FOR_THE_MONTH_OF_NO.GetValueOrDefault(0),
            NatureOfPayment = GetTaxCode().FirstOrDefault(y => y.ATCCode == x.ATC_CODE).NatureOfPayment,
            ForTheYearOf = GetUploadFileDetails(x.UL_ID).FOR_THE_YEAR
        });

        return E2307Details;
    }

Changes:

  • I removed the AsEnumerable, so that the WHERE is executed in the database
  • Then added a ToListAsync, that asynchrounously gets all matching records
  • An extra pair of ( ) around the previous expression, so that the Select works on the List, not on a Task (where it doesn't work)

Remarks

  • Right now you are calling GetUploadFileDetails(x.UL_ID) three times per record. That can probably be optimized.
  • You may want to add another .ToList() at the end of that query.
  • Related