具有分组,空值和许多联接的Linq查询

时间:2018-08-01 15:04:15

标签: c# linq left-join

我的目标是这样的对象

  • 列表:
    • Valueid
    • 分配给上述值的产品列表(所有值可以有很多产品,但有些没有)

查询:

var test = (from values in GetAllValues

                    join productValues in context.ProductValue on values.ValueId equals productValues.ValueId into pv
                    from productValues in pv.DefaultIfEmpty()

                    join products in context.Products on productValues.ProductId equals products.ProductId into p
                    from products in p.DefaultIfEmpty()

                    select new AdminSelectionProductValue()
                    {
                        ValueId = values.ValueId,
                        Value = values.Value,
                        ProductList = p.Select(x=> new P(x.ProductId, x.Name)).ToList(),
                    } into aspv

                    group aspv by aspv.ValueId into g
                    select g
    ).ToList();

帮助程序类:

public class AdminSelectionProductValue
{
    public int ValueId { get; set; }
    public string Value { get; set; }
    public List<P> ProductList { get; set; }

    public class P
    {
        public int ProductId { get; set; }
        public string Name { get; set; }

        public P(int productId, string name)
        {
            this.ProductId = productId;
            this.Name = name;
        }
    }
}

我可以接受匿名类型。

我的结果: My result:

请帮助Linq查询,我不擅长。

样本数据:

+---------+------------------+-------------------+
| ValueId | Value            | ProductList       |
+         +                  +-------------------+
|         |                  | ProductId | Name  |
+---------+------------------+-----------+-------+
| 1       | Wheel            | 1         | 16"   |
+         +                  +-----------+-------+
|         |                  | 2         | 20"   |
+         +                  +-----------+-------+
|         |                  | 3         | 24"   |
+---------+------------------+-----------+-------+
| 2       | Front Derailleur | 8         | Acer  |
+         +                  +-----------+-------+
|         |                  | 9         | Deore |
+---------+------------------+-----------+-------+
| 3       | Color            | null      | null  |
+---------+------------------+-----------+-------+
| 4       | Size             | null      | null  |
+---------+------------------+-----------+-------+

答案: 我之所以没有添加答案,是因为我只用临qu来管理目标。

List<AdminSelectionProductValue> listAspv = new List<AdminSelectionProductValue>();
            foreach (var v in GetAllValues)
            {
                var productValues = new EFProductValueRepository(context).GetProductValueByValueId(v.ValueId);

            AdminSelectionProductValue aspv = new AdminSelectionProductValue();
            aspv.Value = v.Value;
            aspv.ValueId = v.ValueId;
            aspv.ProductList = (from pv in productValues join p in context.Products on pv.ProductId equals p.ProductId select new PoductHelper(pv.ProductId, p.Name)).ToList();
            listAspv.Add(aspv);
        }
        return listAspv;

0 个答案:

没有答案