Lock Lock - 1 year ago 36
C# Question

How to compare 2 lists based on separate keys and output a new list with differences

I have a

List<Ap21Stock>
where by Ap21Stock looks like this:

public class Ap21Stock
{
public Ap21Product Product { get; set; }
public int FreeStock { get; set; }
}


with Ap21Product looking like:

public class Ap21Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string StyleCode { get; set; }
public string ColourCode { get; set; }
public string SizeCode { get; set; }
}


I also have another
List<Product>
that looks like this:

public class Product
{
public string SellerSku { get; set; }
public string ShopSku { get; set; }
public int Quantity { get; set; }
public int FulfillmentByNonSellable { get; set; }
}


I need to produce a separate list:
- The key of the first list is the concatenation of StyleCode, ColourCode and SizeCode separated by a period (.). This matches up to the
SellerSku
property in the second list.
- Only output values where
FreeStock
!=
Quantity
.

Basically, I have 2 stock lists from separate system and need to lookup the differences, however both are keyed differently (first is keyed in separate fields whereas the second is one field keyed in a single string).

How would I do this?
I tried the below but it doesn't feel right (and possibly wrong and inefficient as I am getting zero results in my last dictionary which I haven't yet looked into):

Dictionary<string, int> apparel21StocksKeyed = new Dictionary<string, int>();
foreach ( var product in apparel21Stocks )
{
apparel21StocksKeyed.Add(string.Concat(product.StyleCode, ".", product.ColourCode, ".", product.SizeCode, "."), product.FreeStock);
}

//loop over the products, producing a list to send back to The Iconic
Dictionary<string, int> productsToUpdate = new Dictionary<string, int>();
foreach (var stock in iconicStocks)
{
if (apparel21StocksKeyed.ContainsKey(stock.SellerSku) && apparel21StocksKeyed[stock.SellerSku] != stock.Quantity)
{
productsToUpdate.Add(stock.SellerSku, apparel21StocksKeyed[stock.SellerSku]);
}
}

Answer Source

Try this :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Ap21Stock> stock = new List<Ap21Stock>();
            List<Product> products = new List<Product>();

            var results = from s in stock
                          join p in products on string.Join(".", new string[] { s.ProductId.ToString(), s.StyleCode, s.ColourCode }) equals p.SellerSku
                          where s.FreeStock != p.Quantity 
                          select new { id = s.ProductId, sku = p.SellerSku };

        }
    }
    public class Ap21Stock
    {
        public int ProductId { get; set; }
        public string StyleCode { get; set; }
        public string ColourCode { get; set; }
        public string SizeCode { get; set; }
        public int FreeStock { get; set; }
    }
    public class Product
    {
        public string SellerSku { get; set; }
        public string ShopSku { get; set; }
        public int Quantity { get; set; }
        public int FulfillmentByNonSellable { get; set; }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download