我有一个List<Ap21Stock>
,其中Ap21Stock看起来像这样:
public class Ap21Stock
{
public Ap21Product Product { get; set; }
public int FreeStock { get; set; }
}
与Ap21Product看起来像:
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; }
}
我还有另一个List<Product>
,如下所示:
public class Product
{
public string SellerSku { get; set; }
public string ShopSku { get; set; }
public int Quantity { get; set; }
public int FulfillmentByNonSellable { get; set; }
}
我需要制作一份单独的清单:
- 第一个列表的键是StyleCode,ColourCode和SizeCode的串联,用句点(。)分隔。这与第二个列表中的SellerSku
属性匹配。
- 仅输出值FreeStock
!= Quantity
。
基本上,我有来自不同系统的2个库存清单,需要查找差异,但两者的键控方式不同(首先键入单独的字段,而第二个是键入单个字符串的一个字段)。
我该怎么做? 我尝试了以下但是感觉不对(可能是错误和低效的,因为我在上一本字典中得到的结果为零,我还没有考虑过):
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]);
}
}
答案 0 :(得分:1)
试试这个:
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; }
}
}