Hi @Mitch McConnell , welcome to Microsoft Q&A.
You could use linq to quickly select the value you need.
You could try the following code to get what you wanted.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp2
{
public class ApiTwoDto
{
public List<Results> results { get; set; }
}
public class Results
{
public string displaySku { get; set; }
public int qtyAvailable { get; set; }
}
public class ApiOneDto
{
public int parent_id { get; set; }
public int? id { get; set; }
public string sku { get; set; }
public int? stock_quantity { get; set; }
}
internal class Program
{
static void Main(string[] args)
{
ApiTwoDto apiTwoDto = new ApiTwoDto
{
results = new List<Results>
{
new Results { displaySku = "SKU1", qtyAvailable = 1 },
new Results { displaySku = "SKU2", qtyAvailable = 2 },
new Results { displaySku = "SKU3", qtyAvailable = 4 },
new Results { displaySku = "SKU4", qtyAvailable = 5 },
new Results { displaySku = "SKU5", qtyAvailable = 8 }
}
};
List<ApiOneDto> varDto = new List<ApiOneDto>
{
new ApiOneDto { parent_id = 1, id = 1, sku = "SKU1", stock_quantity = 1 },
new ApiOneDto { parent_id = 2, id = 2, sku = "SKU2", stock_quantity = 3 },
new ApiOneDto { parent_id = 3, id = 3, sku = "SKU3", stock_quantity = 4 },
new ApiOneDto { parent_id = 4, id = 4, sku = "SKU4", stock_quantity = 7 },
new ApiOneDto { parent_id = 5, id = 5, sku = "SKU5", stock_quantity = 9 }
};
//USE LINQ
var mismatchedSkus = apiTwoDto.results
.Where(two => varDto.Any(one => one.sku == two.displaySku && one.stock_quantity != two.qtyAvailable))
.Select(two => new { SKU = two.displaySku, QtyAvailable = two.qtyAvailable });
foreach (var sku in mismatchedSkus)
{
Console.WriteLine($"SKU: {sku.SKU}, QtyAvailable: {sku.QtyAvailable}");
}
//USE LINQ
var mismatchedGroups = varDto
.Join(apiTwoDto.results,
one => one.sku,
two => two.displaySku,
(one, two) => new { OneDto = one, TwoDto = two })
.Where(group => group.OneDto.stock_quantity != group.TwoDto.qtyAvailable)
.GroupBy(group => new { group.OneDto.parent_id, group.OneDto.id, group.OneDto.sku, group.OneDto.stock_quantity })
.Select(group => group.Key);
foreach (var group in mismatchedGroups)
{
Console.WriteLine($"parent_id: {group.parent_id}, id: {group.id}, sku: {group.sku}, stock_quantity: {group.stock_quantity}");
}
Console.ReadLine();
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.