How To Get All Data From DTO That Does Not Match Seperate DTO

Mitch McConnell 41 Reputation points
2023-04-17T00:05:13.1633333+00:00

Hello - I have two DTO's like this ``

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; }
}

List<ApiOneDto> varDto = new List<ApiOneDto>();

And I want to get all of the SKU's and Quantities from ApiTwoDto where the qtyAvailable for the sku DOES NOT MATCH the stock_quantity for the sku in ApiOneDto How would I get that? --- again - I want to get every sku and qtyAvaliable from ApiTwoDto where the stock_quantity for the sku does not match ApiOneDto

Developer technologies | .NET | Other
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-04-17T13:49:45.67+00:00

    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();
            }
        }
    }
    
    

    enter image description here

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.