C# 具有分组、空值和多个联接的Linq查询
我的目标是这样的:C# 具有分组、空值和多个联接的Linq查询,c#,linq,left-join,C#,Linq,Left Join,我的目标是这样的: 名单: 瓦卢伊德 价值观 分配给上述值的产品列表(所有值可以有许多产品,但有些没有) 查询: var test = (from values in GetAllValues join productValues in context.ProductValue on values.ValueId equals productValues.ValueId into pv from produ
- 名单:
- 瓦卢伊德
- 价值观
- 分配给上述值的产品列表(所有值可以有许多产品,但有些没有)
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;
}
}
}
+---------+------------------+-------------------+
| 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 |
+---------+------------------+-----------+-------+
助手类:
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;
}
}
}
+---------+------------------+-------------------+
| 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 |
+---------+------------------+-----------+-------+
回答:
我并没有添加帖子的答案,因为我并没有只用临朐来管理aim
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;
List listAspv=new List();
foreach(GetAllValues中的var v)
{
var productValues=new EFProductValueRepository(context).GetProductValueByValueId(v.ValueId);
AdminSelectionProductValue aspv=新的AdminSelectionProductValue();
aspv.值=v.值;
aspv.ValueId=v.ValueId;
aspv.ProductList=(从productValues中的pv在上下文中加入p。pv.ProductId上的产品等于p.ProductId选择新的PoductHelper(pv.ProductId,p.Name))。ToList();
列表aspv.Add(aspv);
}
返回listAspv;
您使用了左联接,因此某些ProductList没有数据。好的,我的目标结果是:简单的列表中有值id值和产品分配到值的列表,但某些值没有产品,因此它们将有空列表。我想避免出现像图中一样的情况,即“Rama”对每个项目都有相同的值,单独列表中有一个项目。据我所知,您希望将以下值分组,对吗?为了更清楚,您可以发布一些示例数据,并显示预期结果。您想将以下值分组,对吗?-是的,必须修改该行:按aspv.Value将aspv分组为g。您可以访问此链接,更多地了解Linq中的组: