我正在尝试加入三个linq查询的结果。类型如下:
orderItems
是List<OrderLineItem>
transitTimes
是Dictionary<int,int>
Key = SiteId
和Value = TransitDays
shippingPriority
是Dictionary<int,int>
Key = DefaultPriority
和Value = SiteId
public class OrderLineItem
{
public decimal OrderLineWorkId { get; set; }
public string Sku { get; set; }
public string SiteId { get; set; }
public char FlagSlapType { get; set; }
public char FlagTruck { get; set; }
public string SkuType { get; set; }
public int QtyOrdered { get; set; }
public int QtySellable { get; set; }
}
public class OrderAllocation
{
public int SiteId { get; set; }
public int TransitDays { get; set; }
public int QtyItemsInStock { get; set; }
public int QtyParcelInStock { get; set; }
public int DefaultPriority { get; set; }
}
这是我的linq查询,但它总是返回0结果。不知道我哪里出错了?
var results = (from i in orderItems
join t in transitTimes on i.SiteId equals t.Key.ToString()
join d in shippingPriority on t.Key equals d.Value
group i by i.SiteId into g
select new OrderAllocation()
{
SiteId = Convert.ToInt32(g.Key),
TransitDays = transitTimes.Select(x => x.Value).FirstOrDefault(),
QtyItemsInStock = g.Count(e => e.QtySellable >= e.QtyOrdered),
QtyParcelInStock = g.Count(e => e.QtySellable >= e.QtyOrdered && e.FlagSlapType != 'Y'),
DefaultPriority = defaultShippingPriority.Select(x => x.Key).FirstOrDefault()
}).OrderBy(x => x.SiteId).ToList();
更新
我发现错误,SiteId
类型string
的值为前缀0
(即01
,02
等)因此,不会加入1
,2
等值。将类型从string
更改为int
,现在可以获得结果。抱歉误报。
答案 0 :(得分:0)
问题可能与数据类型的差异有关:
(from i in orderItems
join t in transitTimes on i.SiteId equals t.Key.ToString()
orderItems将SiteId定义为字符串
public string SiteId { get; set; }
而transitTimes的密钥为int
Dictionary<int,int>
int 与字符串的连接条件可能无法提供您期望的结果。