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

查看:93
本文介绍了我的GroupBy查询有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的GroupBy查询有什么问题吗?

Hello all what is wrong with my GroupBy query ?

我有以下课程:

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

但是我那里的val类型是int,而不是我可以在那里进行分组的范围val.SelectMany(v => v).ToList();任何想法我的代码有什么问题吗?

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)

侧面说明:属性名称为Posiotion

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

这篇关于我的GroupBy查询有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆