NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

I get it now, bro! You want to:

1. Extract unique Supplier_Id values and call API once with Supplier_Id eq xxx or yyy.


2. Extract unique (Supplier_Id, Inv_Mast_Uid) pairs and call API once with Supplier_Id eq xxx and Inv_Mast_Uid eq yyy.


3. Merge all data from 4 API calls into a single final model.




---

🔥 Optimized Solution

Step 1: Fetch Initial Invoice Data

// Fetch header and line details
HttpResponseMessage headerResponse = await FetchP21InvoiceDetailsAsync(baseHeaderUrl, result.AccessToken!);
HttpResponseMessage lineResponse = await FetchP21InvoiceDetailsAsync(baseLineUrl, result.AccessToken!);

// Ensure both API calls succeeded
if (!headerResponse.IsSuccessStatusCode || !lineResponse.IsSuccessStatusCode)
return; // Handle error case

// Read JSON response
string headerJson = await headerResponse.Content.ReadAsStringAsync();
string lineJson = await lineResponse.Content.ReadAsStringAsync();

// Parse JSON to Lists
List<EpicorP21HdrModel> headerData = JsonConvert.DeserializeObject<List<EpicorP21HdrModel>>(headerJson)!;
List<EpicorP21LineModel> lineData = JsonConvert.DeserializeObject<List<EpicorP21LineModel>>(lineJson)!;

// Combine Header and Line Data
var combinedInvoiceData = from headers in headerData
join lines in lineData on headers.invoice_no equals lines.invoice_no
select new EpicorP21CombinedModel
{
Bill2Name = headers.bill2_name,
Bill2Address1 = headers.bill2_address1,
Bill2Address2 = headers.bill2_address2,
LineNo = lines.line_no,
ManufacturerId = lines.supplier_id,
DistProdId = lines.item_id,
DistProdDesc = lines.item_desc,
ExtendedAqCost = lines.cogs_amount,
InvMastUid = lines.inv_mast_uid // Store inv_mast_uid for second API
};

List<EpicorP21CombinedModel> invoiceDataList = combinedInvoiceData.ToList();


---

Step 2: Fetch Supplier Details API (1st API Call)

// Extract Unique Supplier IDs
var uniqueSupplierIds = invoiceDataList
.Select(x => x.ManufacturerId)
.Where(id => !string.IsNullOrEmpty(id))
.Distinct()
.ToList();

// Construct Supplier Filter
string supplierFilter = uniqueSupplierIds.Count > 0
? string.Join(" or ", uniqueSupplierIds.Select(id => $"Supplier_Id eq '{id}'"))
: "";

List<SupplierModel> supplierData = new List<SupplierModel>();

if (!string.IsNullOrEmpty(supplierFilter))
{
string supplierDetailsUrl = $"{baseSupplierUrl}?$filter={Uri.EscapeDataString(supplierFilter)}";
HttpResponseMessage supplierResponse = await FetchP21InvoiceDetailsAsync(supplierDetailsUrl, result.AccessToken!);

if (supplierResponse.IsSuccessStatusCode)
{
string supplierJson = await supplierResponse.Content.ReadAsStringAsync();
supplierData = JsonConvert.DeserializeObject<List<SupplierModel>>(supplierJson)!;
}
}


---

Step 3: Fetch Supplier & Inv_Mast_Uid Details API (2nd API Call)

// Extract Unique (Supplier ID, Inv Mast UID) Pairs
var uniqueSupplierUidPairs = invoiceDataList
.Where(x => !string.IsNullOrEmpty(x.ManufacturerId) && !string.IsNullOrEmpty(x.InvMastUid))
.Select(x => new { x.ManufacturerId, x.InvMastUid })
.Distinct()
.ToList();

// Construct Supplier & UID Filter
string supplierUidFilter = uniqueSupplierUidPairs.Count > 0
? string.Join(" or ", uniqueSupplierUidPairs.Select(x => $"(Supplier_Id eq '{x.ManufacturerId}' and Inv_Mast_Uid eq '{x.InvMastUid}')"))
: "";

List<SupplierUidModel> supplierUidData = new List<SupplierUidModel>();

if (!string.IsNullOrEmpty(supplierUidFilter))
{
string supplierUidDetailsUrl = $"{baseSupplierUidUrl}?$filter={Uri.EscapeDataString(supplierUidFilter)}";
HttpResponseMessage supplierUidResponse = await FetchP21InvoiceDetailsAsync(supplierUidDetailsUrl, result.AccessToken!);

if (supplierUidResponse.IsSuccessStatusCode)
{
string supplierUidJson = await supplierUidResponse.Content.ReadAsStringAsync();
supplierUidData = JsonConvert.DeserializeObject<List<SupplierUidModel>>(supplierUidJson)!;
}
}


---

Step 4: Merge All Data into Final Model

var finalCombinedData = from invoice in invoiceDataList
join supplier in supplierData on invoice.ManufacturerId equals supplier.SupplierId into supplierJoin
from supplierInfo in supplierJoin.DefaultIfEmpty()
join supplierUid in supplierUidData on new { invoice.ManufacturerId, invoice.InvMastUid }
equals new { supplierUid.SupplierId, supplierUid.InvMastUid } into supplierUidJoin
from supplierUidInfo in supplierUidJoin.DefaultIfEmpty()
select new EpicorP21FinalModel
{
Bill2Name = invoice.Bill2Name,
Bill2Address1 = invoice.Bill2Address1,
Bill2Address2 = invoice.Bill2Address2,
LineNo = invoice.LineNo,
ManufacturerId = invoice.ManufacturerId,
DistProdId = invoice.DistProdId,
DistProdDesc = invoice.DistProdDesc,
ExtendedAqCost = invoice.ExtendedAqCost,
SupplierName = supplierInfo?.SupplierName ?? "Unknown", // From First API
AdditionalField = supplierUidInfo?.SomeField ?? "N/A" // From Second API
};

List<EpicorP21FinalModel> finalDataList = finalCombinedData.ToList();


---

🔥 Why This Approach?

✅ Only 2 Extra API Calls (instead of calling API in a loop for each ID).
✅ Efficient Filtering (Supplier_Id eq xxx or yyy, Supplier_Id eq xxx and Inv_Mast_Uid eq yyy).
✅ Final Merge Using LINQ for a single complete model.

Now, finalDataList contains all required merged data in one go! 🚀 Let me know if you need any changes, bro! 💪🔥

     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.