LINQ分组查询

14 浏览
0 Comments

LINQ分组查询

我最初提出了一个问题,因为我在一个类似的查询中遇到了一个错误,并找到了解决方法,现在我有一个问题/需要一点帮助来理解如何正确组织返回的结果。这将用于WEB API,并且需要以特定的方式对输出进行分组,但我无法完全理解。\n类 -\n

public class GoodInWarehouseBM
{
    /// 
    /// 托盘编号
    /// 
    public string pallet_identifier { get; set; }
    public List shipment_items { get; set; }
    public class ShipmentItems
    {
        /// 
        /// SKU编码
        /// 
        public string sku { get; set; }
        /// 
        /// 托盘上的产品数量
        /// 
        public decimal stock_qty { get; set; }
        /// 
        /// 产品描述
        /// 
        public string description { get; set; }
    }
}

\n方法 -\n

public IQueryable> 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
                      {
                          new GoodInWarehouseBM.ShipmentItems
                          {
                              sku = productcodess.variantcode,
                              stock_qty = consigliness.issueqty,
                              description = products.proddesc
                          }
                      }
                  }).GroupBy(x => x.pallet_identifier);
    return entity;
}

\n输出 -\n[\n [\n {\n \"pallet_identifier\": \"FS300057058\",\n \"shipment_items\": [\n {\n \"sku\": \"051657\",\n \"stock_qty\": 1,\n \"description\": \"BELT 1.25\\\" 5028\"\n }\n ]\n },\n {\n \"pallet_identifier\": \"FS300057058\",\n \"shipment_items\": [\n {\n \"sku\": \"10121781\",\n \"stock_qty\": 1,\n \"description\": \"SLAZ CREW SWEAT\"\n }\n ]\n },\n ...\n ]\n]\n\n期望的输出 -\n[\n {\n \"pallet_identifier\": \"FS300057058\",\n \"shipment_items\": [\n {\n \"sku\": \"051657\",\n \"stock_qty\": 1,\n \"description\": \"BELT 1.25\\\" 5028\"\n },\n {\n \"sku\": \"10121781\",\n \"stock_qty\": 1,\n \"description\": \"SLAZ CREW SWEAT\"\n }\n ]\n },\n ...\n]\n\n我似乎在如何实际获得这个结果上陷入了困境,任何帮助和指导将不胜感激。

0
0 Comments

LINQ Grouping是一个常见的查询操作,用于将数据集按照指定的条件分组。然而,在实际应用中可能会出现一些问题,需要进行相应的调整和解决。

在上面提供的代码示例中,首先需要对数据进行分组操作,然后选择分组后的结果。具体的代码如下:

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

需要注意的是,这里的方法返回类型应该是`List`,而不是`IQueryable>`。可以通过修改方法的返回类型来解决这个问题。

另外,还可以使用传统的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();

需要注意的是,在这种方式下,可以直接选择`a.productcodess.variantcode`等属性,而不需要将它们添加到分组中。

在问题的回答中,还提到了关于`IEnumerable`和`ToList`的使用。其中,`IEnumerable`是一个接口,而`ToList`是一个具体的类。可以通过添加`.ToList()`来将`IEnumerable`转换为`List`。

最后,回答中还提供了一些相关的链接,可以帮助理解`IEnumerable`和`List`之间的区别。

通过以上的整理,我们可以清晰地了解到LINQ Grouping查询的问题出现原因以及解决方法。在实际应用中,我们可以根据具体情况选择合适的方式来进行查询操作。

0
0 Comments

问题:LINQ查询中的分组(LINQ Grouping a query)出现的原因和解决方法。

原因:

在LINQ查询中,当需要对结果进行分组操作时,可能会遇到一些问题。在给定的代码示例中,原始查询可能没有正确地分组数据,导致结果不符合预期。

解决方法:

为了解决这个问题,可以重新调整查询的顺序,先进行分组(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的语法。

在C#语法中进行连接操作时,通常使用导航属性。这在99%的情况下都能够满足需求,但是如果必须执行连接操作,使用SQL语法的方式确实比使用笨拙的GroupJoin函数更易读。

在LINQ查询中进行分组操作可能会导致结果不符合预期。为了解决这个问题,可以重新调整查询的顺序,先进行分组操作,然后再选择每个分组并将其转换成所需的对象。使用SQL语法的LINQ查询可能更易读,但建议尽可能学习使用基于Lambda的语法。在大多数情况下,使用导航属性进行连接操作可以满足需求,但在必须执行连接操作时,SQL语法的方式可能更易读。

0