连接其他三个查询的结果的linq查询

时间:2017-04-12 12:20:46

标签: linq

我正在尝试加入三个linq查询的结果。类型如下:

orderItemsList<OrderLineItem>

transitTimesDictionary<int,int> Key = SiteIdValue = TransitDays

shippingPriorityDictionary<int,int> Key = DefaultPriorityValue = 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(即0102等)因此,不会加入12等值。将类型从string更改为int,现在可以获得结果。抱歉误报。

1 个答案:

答案 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 字符串的连接条件可能无法提供您期望的结果。