This is my code, here I need make "GetId" API call in parallel for better performance for my custom API, kindly let me know how do I make this.
C#
foreach(ResponseModel AccountSwap in invalidRecords) {
this.ValidatePolicyExist(AccountSwap, bpValidPolicyDataTable);
}
private void ValidatePolicyExist(ResponseModel policyDetails, DataTable ValidDataTable) {
if (this.IsPolicyExist(policyDetails.CorrectPolicyNumber)) {
var row = ValidDataTable.NewRow();
row["IncorrectReference"] = policyDetails.IncorrectNumber;
row["CorrectReference"] = policyDetails.CorrectNumber;
ValidDataTable.Rows.Add(row);
}
}
private bool IsPolicyExist(string policyNumber) {
var accountId = GetId(policyNumber).Result;
// This API will return BIGINT number as a response if the records exist
return accountId != null;
}
public class ResponseModel {
public string IncorrectNumber {
get;
set;
}
public string CorrectNumber {
get;
set;
}
}Here please let me know how I make the “GetId()” API call in parallel call, by doing this my API will be bit faster, kindly help me. I tried doing without parallel it takes 28 seconds for 100 records, but this is more time – we need to reduce to less than 10 second – kindly let me know

Mohammad HussainRank #513 · 4.8K reputationPosted Aug 28, 2023, 4:56 AM EDT
foreach (ResponseModel AccountSwap in invalidRecords) { this.ValidatePolicyExistParallel(AccountSwap, bpValidPolicyDataTable); } private void ValidatePolicyExistParallel(ResponseModel policyDetails, DataTable ValidDataTable) { Parallel.ForEach(invalidRecords, AccountSwap => { if (this.IsPolicyExistParallel(AccountSwap.CorrectPolicyNumber)) { var row = ValidDataTable.NewRow(); row["IncorrectReference"] = policyDetails.IncorrectNumber; row["CorrectReference"] = policyDetails.CorrectNumber; ValidDataTable.Rows.Add(row); } }); } private bool IsPolicyExistParallel(string policyNumber) { var accountId = GetId(policyNumber).Result; // This API will return BIGINT number as a response if the records exist return accountId != null; }Cr BhargaviRank #420 · 5.9K reputationPosted Aug 28, 2023, 1:21 AM EDT
foreach (ResponseModel AccountSwap in invalidRecords) { await this.ValidatePolicyExistAsync(AccountSwap, bpValidPolicyDataTable); } private async Task ValidatePolicyExistAsync(ResponseModel policyDetails, DataTable ValidDataTable) { if (await this.IsPolicyExistAsync(policyDetails.CorrectPolicyNumber)) { var row = ValidDataTable.NewRow(); row["IncorrectReference"] = policyDetails.IncorrectNumber; row["CorrectReference"] = policyDetails.CorrectNumber; ValidDataTable.Rows.Add(row); } } private async Task IsPolicyExistAsync(string policyNumber) { var accountId = await GetIdAsync(policyNumber); // This API will return BIGINT number as a response if the records exist return accountId != null; } private async Task GetIdAsync(string policyNumber) { // Implement the asynchronous logic to call the "GetId" API // and return the result }