繁体   English   中英

LINQ分组查询

[英]LINQ Grouping a query

我最初问一个问题,因为我遇到类似的查询并出错,我发现可以解决此问题,现在我有一个问题/需要一点帮助,以了解如何为退货制定正确的组,这将适用于WEB API,并需要以某种方式将输出分组,我不能完全到达那里。

类-

public class GoodInWarehouseBM
{
    /// <summary>
    /// Pallet Number
    /// </summary>
    public string pallet_identifier { get; set; }

    public List<ShipmentItems> shipment_items { get; set; }

    public class ShipmentItems
    {
        /// <summary>
        /// SKU Code
        /// </summary>
        public string sku { get; set; }

        /// <summary>
        /// Products on Pallet
        /// </summary>
        public decimal stock_qty { get; set; }
        /// <summary>
        /// Description of Item
        /// </summary>
        public string description { get; set; }

    }
}

方法 -

public IQueryable<IGrouping<string, GoodInWarehouseBM>> GetWarehouseToStoreList(string storeId)
{
    var entity = (from consighdrs in mi9TestEntities.consighdrs
                  join consigdests in mi9TestEntities.consigdests on consighdrs.consignment equals consigdests.consignment
                  join consigliness in mi9TestEntities.consiglines on consigdests.condestint equals consigliness
                      .condestint
                  join productcodess in mi9TestEntities.productcodes on consigliness.varint equals productcodess.varint
                  join products in mi9TestEntities.products on productcodess.prodint equals products.prodint
                  where consigdests.destination == storeId && consighdrs.status == "T"
                  select new GoodInWarehouseBM()
                  {
                      pallet_identifier = consigdests.consignment,
                      shipment_items = new List<GoodInWarehouseBM.ShipmentItems>
                    {new GoodInWarehouseBM.ShipmentItems
                    {
                        sku = productcodess.variantcode,
                        stock_qty = consigliness.issueqty,
                        description = products.proddesc
                    }}

                  }).GroupBy(x => x.pallet_identifier);

    return entity;
}

输出-

[
    [
        {
            "pallet_identifier": "FS300057058",
            "shipment_items": [
                {
                    "sku": "051657",
                    "stock_qty": 1,
                    "description": "BELT 1.25\" 5028"
                }
            ]
        },
        {
            "pallet_identifier": "FS300057058",
            "shipment_items": [
                {
                    "sku": "10121781",
                    "stock_qty": 1,
                    "description": "SLAZ CREW SWEAT"
                }
            ]
        },

想要的输出-

[
  {
    "pallet_identifier": "FS300057058",
    "shipment_items": [
            {
              "sku": "051657",
              "stock_qty": 1,
              "description": "BELT 1.25\" 5028"
            },
            {
              "sku": "10121781",
              "stock_qty": 1,
              "description": "SLAZ CREW SWEAT"
            }
        ]
    }
]

我似乎在这里总结如何实际获得该结果,所有帮助和指针都将不胜感激。

重新排序事物,以便您按组分组,然后将每个组选择到所需的对象中:

...
.GroupBy(x => x.consigdests.consignment)
.Select(x => new GoodInWarehouseBM
{
    pallet_identifier = x.Key,
    shipment_items = x.Select(i => new GoodInWarehouseBM.ShipmentItems
    {
        sku = x.productcodess.variantcode,
        stock_qty = x.consigliness.issueqty,
        description = x.products.proddesc
    }
});

您可能不得不稍微弄些语法,因为我不习惯类似SQL的LINQ语法。 我建议尽可能学习使用基于lambda的语法。

您需要在应用如下分组后选择分组结果

...
}).GroupBy(x => x.pallet_identifier)
  .Select(x => new GoodInWarehouseBM { pallet_identifier = x.Key, shipment_items = x.ToList() });

并将方法返回类型更改为List<GoodInWarehouseBM>而不是IQueryable<IGrouping<string, GoodInWarehouseBM>>

或者您可以使用传统的linq查询,

var entity = (from consighdrs in mi9TestEntities.consighdrs
...
...
...
where consigdests.destination == storeId && consighdrs.status == "T"
group new { consigdests, productcodess, consigliness, products } by consigdests.consignment into grp
select new GoodInWarehouseBM
{
    pallet_identifier = grp.Key,
    shipment_items = grp.Select(a => new ShipmentItems
    {
        sku  = a.productcodess.variantcode,
        stock_qty = a.consigliness.issueqty,
        description = a.products.proddesc
    })  
}).ToList();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM