【问题标题】:group by on dictionary with custome class key,value objects using linq使用 linq 对具有自定义类键、值对象的字典进行分组
【发布时间】:2017-05-19 08:16:11
【问题描述】:

我有一个要求,我必须对字典进行分组,并根据分组将结果行放入另一个具有键、值对、值是列表(位置)的字典中。

下面的代码 sn-p 解释了我的问题陈述:

public class Positions
{
    public string Value1 { get; set; }
    public int Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
    public string Value5 { get; set; }
    public string Value6 { get; set; }
    public string Value7 { get; set; }
    public string Value8 { get; set; }
    public string Value9 { get; set; }
    public string Value10 { get; set; }
    public string Value11 { get; set; }
    public string Value12 { get; set; }
}

public struct PositionKey
{
    public string Value1 { get; set; }
    public int Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
    public string Value5 { get; set; }
    public string Value6 { get; set; }

    public override int GetHashCode()
    {
        return (Value1 != null ? Value1.GetHashCode() : 1)
               ^ (Value2 > 0 ? Value2.GetHashCode() : 1)
               ^ (Value3 != null ? Value3.GetHashCode() : 1)
               ^ (Value4 != null ? Value4.GetHashCode() : 1)
               ^ (Value5 != null ? Value5.GetHashCode() : 1)
               ^ (Value6 != null ? Value6.GetHashCode() : 1);
    }

    public override bool Equals(object obj)
    {
        PositionKey compositeKey = (PositionKey)obj;
        return Value1 == compositeKey.Value1
                 && Value2 == compositeKey.Value2
                 && Value3 == compositeKey.Value3
                 && Value4 == compositeKey.Value4
                 && Value5 == compositeKey.Value5
                 && Value6 == compositeKey.Value6;
    }
}


public struct GroupbyPositionKey
{
    public string Value1 { get; set; }
    public int Value2 { get; set; }
    public override int GetHashCode()
    {
        return (Value1 != null ? Value1.GetHashCode() : 1)
               ^ (Value2 > 0 ? Value2.GetHashCode() : 1);
    }

    public override bool Equals(object obj)
    {
        PositionKey compositeKey = (PositionKey)obj;
        return Value1 == compositeKey.Value1
                 && Value2 == compositeKey.Value2;
    }
}

public class program
{
    /*         
        Value1  Value2  Value3  Value4  Value5  Value6  Value7  Value8  Value9  Value10  Value11    Value12
        -------------------------------------------------------------------------------------------------
        v1      1       val1    val2    val3    val4    val5    val6    val7    val8     val9       val10
        v1      1       val2    val3    val4    val5    val6    val7    val8    val9     val10      val11
        v3      4       val3    val4    val5    val6    val7    val8    val9    val10    val11      val12
        v3      4       val4    val5    val6    val7    val8    val9    val10   val11    val12      val13
        v3      5       val5    val6    val7    val8    val9    val10   val11   val12    val13      val14
        v4      6       val6    val7    val8    val9    val10   val11   val12   val13    val14      val15
        v4      6       val7    val8    val9    val10   val11   val12   val13   val14    val15      val16
        v4      7       val8    val9    val10   val11   val12   val13   val14   val15    val16      val17
        v4      7       val9    val10   val11   val12   val13   val14   val15   val16    val17      val18


        Group by - Value1   Value2  Value3  Value4  Value5  Value6  Value7  Value8  Value9  Value10  Value11    Value12
        Get List of the rows after the grouping on the basis of group by Value1,Value2
     */
    public static void main()
    {
        Dictionary<PositionKey, Positions> dictPositons = new Dictionary<PositionKey, Positions>();

        Positions obj = new Positions();
        obj.Value1 = "v1";
        obj.Value2 = 1;
        obj.Value3 = "val1";
        obj.Value4 = "val2";
        obj.Value5 = "val3";
        obj.Value6 = "val4";
        obj.Value7 = "val5";
        // ........ and so on.. and so forth...for all the objects.

        /*
            I have a datatable as above and i am inserting the rows in the collection object of class <Positions> with respective key
            and add the same to the dictionary.
        */

        PositionKey key = new PositionKey();
        key.Value1 = "v1";
        key.Value2 = 1;
        key.Value3 = "val3";
        key.Value4 = "val4";
        key.Value5 = "val5";
        key.Value6 = "val6";

        if (!dictPositons.ContainsKey(key))
        {
            dictPositons.Add(key, obj);  // this dictionary would have the raw data which would have all the rows from the table as a dictionary collection
        }


        /*
         Now I have to create another dictionary which gives me a list of all the records by grouping on the basis 
         of the key <GroupbyPositionKey> which is nothing but <Value1,Value2>

        The resulting dictionary should look like Dictionary<GroupbyPositionKey,List<Positions>>
         */
        Dictionary<GroupbyPositionKey, List<Positions>> result = new Dictionary<GroupbyPositionKey, List<Positions>>();
        result = dictPositions.GroupBy(....);    // not sure how ? 
    }
}

在结果中,我希望有另一个字典,其中键对象为 value1、value2 和 value 对象为 List(Positions)。我不知道如何在字典中接近 group by 以获得所需的结果。

我已经通过手动循环字典来实现这个结果,然后根据新键选择并插入到另一个字典中。但我想知道是否有一种 LINQ 方法可以做到这一点,它会更短。

【问题讨论】:

    标签: linq dictionary group-by


    【解决方案1】:

    在您的GroupBy 语句中,您需要创建一个新的GroupbyPositionKey 类,然后使用ToDictionary 方法来选择您的密钥和列表

        Dictionary<GroupbyPositionKey, List<Positions>> result = dictPositons
            .GroupBy(x => new GroupbyPositionKey { Value1 = x.Key.Value1, Value2 = x.Key.Value2 })
            .ToDictionary(x => x.Key, x => x.Select(y => y.Value).ToList());
    

    【讨论】:

    • 我的错,感谢您指出这一点。但是我有一组更大的键,即 通过唯一标识所有记录并插入字典来填充第一个字典。之后我想只用两组键来应用分组。我也已经通过手动完成了这一点。所以我需要一种 LINQ 方式。
    • 您能否更新问题以更准确地显示您要解决的问题?
    • 是的,我已经更新了问题,如果还需要更新,请告诉我?
    • 是的,非常感谢,这对我来说就像一个魅力。我又添加了一个调整 -> x.ToList().Select(y => y.Value).ToList() 因为我只想要没有键的位置类对象列表。因为它给了我一个列表 >
    • 啊,是的,我的错误,谢谢。我已经为将来看到此问题的任何人更新了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    相关资源
    最近更新 更多