Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 我的GroupBy查询有什么问题_C#_Linq - Fatal编程技术网

C# 我的GroupBy查询有什么问题

C# 我的GroupBy查询有什么问题,c#,linq,C#,Linq,大家好我的GroupBy查询有什么问题 我有以下课程: public class AssembledPartsDTO { public int PID { get; set; } public McPosition Posiotion { get; set; } public string Partnumber { get; set; } public string ReelID { get; set; } public int BlockId { get

大家好我的GroupBy查询有什么问题

我有以下课程:

public class AssembledPartsDTO
{
    public int PID { get; set; }
    public McPosition Posiotion { get; set; }
    public string Partnumber { get; set; }
    public string ReelID { get; set; }
    public int BlockId { get; set; }
    public List<string> References { get; set; }
}
我正在尝试执行以下查询:

assembledPcb.AssembledParts.GroupBy(entry => new
                        {
                            entry.PID,
                            entry.Posiotion.Station,
                            entry.Posiotion.Slot,
                            entry.Posiotion.Subslot,
                            entry.Partnumber,
                            entry.ReelID,
                            entry.BlockId
                        }).
                        Select( (key , val )=> new AssembledPartsDTO
                            {
                                BlockId = key.Key.BlockId,
                                PID = key.Key.PID,
                                Partnumber = key.Key.Partnumber,
                                ReelID = key.Key.ReelID,
                                Posiotion = new McPosition(key.Key.Station, key.Key.Slot, key.Key.Subslot),
                                References = val <-- ????
                            })
但是我这里的val是int类型的,而不是我可以在那里进行分组的val val.SelectManyv=>v.ToList;知道我的代码有什么错误吗?

的第二个参数是序列中项目的索引。所以在这种情况下,它是组的零基数。您只需选择组,不需要它的索引:

var result = assembledPcb.AssembledParts.GroupBy(entry => new
{
    entry.PID,
    entry.Posiotion.Station,
    entry.Posiotion.Slot,
    entry.Posiotion.Subslot,
    entry.Partnumber,
    entry.ReelID,
    entry.BlockId
})
.Select(g => new AssembledPartsDTO
{
    BlockId = g.Key.BlockId,
    PID = g.Key.PID,
    Partnumber = g.Key.Partnumber,
    ReelID = g.Key.ReelID,
    Posiotion = new McPosition(g.Key.Station, g.Key.Slot, g.Key.Subslot),
    References = g.SelectMany(entry => entry.References)
                  .Distinct()
                  .ToList()
});
假设您想要一个不同引用的列表

旁注:您在属性名处有一个输入错误:Posiotion

的第二个参数是序列中项目的索引。所以在这种情况下,它是组的零基数。您只需选择组,不需要它的索引:

var result = assembledPcb.AssembledParts.GroupBy(entry => new
{
    entry.PID,
    entry.Posiotion.Station,
    entry.Posiotion.Slot,
    entry.Posiotion.Subslot,
    entry.Partnumber,
    entry.ReelID,
    entry.BlockId
})
.Select(g => new AssembledPartsDTO
{
    BlockId = g.Key.BlockId,
    PID = g.Key.PID,
    Partnumber = g.Key.Partnumber,
    ReelID = g.Key.ReelID,
    Posiotion = new McPosition(g.Key.Station, g.Key.Slot, g.Key.Subslot),
    References = g.SelectMany(entry => entry.References)
                  .Distinct()
                  .ToList()
});
假设您想要一个不同引用的列表

旁注:您在属性名Posiotion处有一个输入错误