LINQ分组查询

时间:2019-02-11 09:42:52

标签: c# asp.net-mvc linq asp.net-web-api

最初,我遇到一个类似的查询时遇到了一个错误,但我发现了修复该问题的帮助,现在我有一个问题/需要一点帮助,以了解如何为退货制定正确的群体,这将适用于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"
            }
        ]
    }
]

我似乎在总结如何实际获得该结果,将非常感谢您接受任何帮助和指针。

2 个答案:

答案 0 :(得分:4)

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

...
.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的语法。

答案 1 :(得分:1)

应用类似分组后,您需要选择分组结果

...
}).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();