我是C#的新手。我在mapper类中有一个方法,它接受两个参数List1和List2作为源,并返回一个Model类的List,它有定义的字段。字段来自两种列表类型。
下面是类(SOHMapper)/方法(Map)
public class SOHMapper : ISOHMapper
{
public List<StockonHand> Map(List<WarehouseOnHand> warehouseonhands, List<SPR_SKU> SKus)
{
var Stockonhands = warehouseonhands.Join(SKus, x1 => x1.ItemNumber, x2 => x2.ItemId, (x1, x2) => new
{ x1.Quantity, x1.ProductStyleId, x1.ItemNumber, x2.GNumber, x2.Style }).Join(SKus,
x1 => x1.ProductStyleId, x2 => x2.Style, (x1, x2) => new
{ x1.Quantity, x1.ItemNumber, x2.GNumber }).GroupBy(g => new { g.GNumber, g.ItemNumber })
.Select(n => new StockonHand
{
DFUCode = "\"" + n.Key.GNumber + "\"",
ItemNumber = "\"" + n.Key.ItemNumber + "\"",
CompanyCode = "\"" + "VPAC" + "\"",
NoOfCases = n.Sum(s => s.Quantity),
Date = DateTime.Now.ToString("dd/MM/yyyy")
}).ToList();
return Stockonhands;
}
}
以下是我的Model Class(Stockonhand):
public class StockonHand
{
public string DFUCode { get; set; }
public string ItemNumber { get; set; }
public string CompanyCode { get; set; }
public decimal NoOfCases { get; set; }
public string Date { get; set; }
}
现在这个工作正常,我得到了所需的数据。但要求不是在lambda表达式中使用“Join”方法/关键字。有没有其他方法可以从列表中提取列?有没有办法使用“Where”方法/关键字从两个不同的List中获取字段?
答案 0 :(得分:0)
在方法语法中:
var result = warehouseonhands.SelectMany(w => SKus.Where(s => s.ItemId == w.ItemNumber)
.Select(s => new { w, s }));
在查询语法中:
var result = from w in warehouseonhands
from s in SKus
where w.ItemNumber == s.ItemId
select new { w, s };