联接表中的 LINQ 透视

本文关键字:LINQ 透视 | 更新日期: 2023-09-27 18:37:28

-------------           
Products    |
-------------       
id          |
name        |
price       |
image       |
-------------
-------------
Sizes       |
-------------   
id          |
name        |
quantity    |
id_product  |
-------------

在表 Sizes 中,我在产品的每种尺寸中保持不同的尺寸和数量。例如,我的产品中有一条牛仔裤,但在表格Sizes中,我将它们保持在 M Sizes.Name 和数量 20、Sizes.Name XL 和数量 30 等。

在我的项目中,当我想显示所有数据时,我有这样的数据

id      |name       |price | size_name      | quantity  |
-----------------------------------------------------------
1       jeans       100     M                   20
1       jeans       100     XL                  30
1       jeans       100     S                   45

等。

我想显示的是:

id      |name       |price | S  | M  | XL |
-------------------------------------------------
1       jeans       1000    45    20   30

所以我读到我必须使用枢轴,但不知道如何开始以及下一步该做什么。这是我的一些代码:

var query = from product in context.Products
            join size in context.Sizes on product.ID equals r.Product.ID    
            //what's the next step?    
            select new {  };
dataGridView1.DataSource = query.ToList();

====

==================================

编辑

====

===================================

现在我有这样的 STH

好吧,{id, name}不是钥匙,但似乎还有另一个问题,

var q = (from p in context.Produkty
                     join r in context.Rozmiary
                         on p.ID equals r.Produkt.ID
                         into sizes
                     select new
                     {
                         S = sizes.Where(x => x.Nazwa == NazwaRozmiaru.S).Sum() ?? 0
                     });
           dataGridView1.DataSource = q.ToList();

给我这个

Error   2   'System.Collections.Generic.IEnumerable<Magazynier.Model.Rozmiary>' does not contain a definition for 'Sum' and the best extension method overload 'System.Linq.ParallelEnumerable.Sum(System.Linq.ParallelQuery<decimal?>)' has some invalid arguments C:'Users'Piotr'Downloads'Magazynier (7)'Magazynier'Magazynier'Form1.cs  45  34  Magazynier

而这个

Error   3   Instance argument: cannot convert from 'System.Collections.Generic.IEnumerable<Magazynier.Model.Rozmiary>' to 'System.Linq.ParallelQuery<decimal?>' C:'Users'Piotr'Downloads'Magazynier (7)'Magazynier'Magazynier'Form1.cs  45  34  Magazynier

而这个

Error   4   The type arguments for method 'System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.    C:'Users'Piotr'Downloads'Magazynier (7)'Magazynier'Magazynier'Form1.cs  47  43  Magazynier

我的尺寸(又名 Rozmiary)在 c# 中看起来像这样

public enum NazwaRozmiaru
{
    S,
    M,
    L,
}
public class Rozmiary : KlasaBazowa
{
    public Produkty Produkt { get; set; }
    public NazwaRozmiaru Nazwa { get; set; }
    public int Ilosc { get; set; }
}

联接表中的 LINQ 透视

假设:

  • {id, name}是尺寸的关键
  • 您只想显示 S、M 和 XL 大小。

试试这个:

from product in context.Products
join size in context.Sizes
    on product.ID equals r.Product.ID
    into sizes
select new {
    product.id,
    product.name,
    product.price,
    S = sizes.Where(x => x.name == "S").Select(x => x.quantity).SingleOrDefault(),
    M = sizes.Where(x => x.name == "M").Select(x => x.quantity).SingleOrDefault(),
    XL = sizes.Where(x => x.name == "XL").Select(x => x.quantity).SingleOrDefault()
}

如果{id, name}不是密钥,则将其插入:

 S = sizes.Where(x => x.name == "S").Sum(x => x.quantity) ?? 0

如果要显示所有大小,但不知道会有什么大小,则无法使用 LINQ 获得漂亮的表,但可以这样做:

select new {
    product.id,
    product.name,
    product.price,
    quantitiesBySize = sizes.ToDictionary(size => size.name, size => size.quantity)
}