我的GroupBy查询有什么问题[英] what is wrong with my GroupBy Query

本文是小编为大家收集整理的关于我的GroupBy查询有什么问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

大家好,我的 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.SelectMany(v => v).ToList(); 知道我的代码有什么问题吗?

解决方案

Enumerable.Select 是序列中项目的索引.所以在这种情况下,它是组的(从零开始的)数字.您只想选择组,不需要它的索引:

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

本文地址:https://www.itbaoku.cn/post/1557077.html

问题描述

Hello all what is wrong with my GroupBy query ?

I have following class:

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; }
}

I am trying to perform following query:

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 <-- ????
                            })

But the val that I have there is of type int and not the val of grouping that I can do there val.SelectMany(v => v).ToList(); any idea what is wrong in my code ?

推荐答案

The second parameter of Enumerable.Select is the index of the item in the sequence. So in this case it is the (zero based) number of the group. You just want to select the group, you don't need it's index:

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()
});

(assuming that you want a list of distinct references)

Side-Note: you have a typo at the property-name: Posiotion