使用自定义类键在字典上分组,使用linq值对象

使用自定义类键在字典上分组,使用linq值对象,linq,dictionary,group-by,Linq,Dictionary,Group By,我有一个要求,我必须在字典上做一个group by,并根据group by将结果行放入另一个字典中,该字典具有键、值对,值是一个列表(位置) 下面是解释我的问题陈述的代码片段: public class Positions { public string Value1 { get; set; } public int Value2 { get; set; } public string Value3 { get; set; } public string Valu

我有一个要求,我必须在字典上做一个group by,并根据group by将结果行放入另一个字典中,该字典具有键、值对,值是一个列表(位置)

下面是解释我的问题陈述的代码片段:

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 ? 
    }
}
公共类职位
{
公共字符串值1{get;set;}
公共int值2{get;set;}
公共字符串值3{get;set;}
公共字符串值4{get;set;}
公共字符串值5{get;set;}
公共字符串值6{get;set;}
公共字符串值7{get;set;}
公共字符串值8{get;set;}
公共字符串值9{get;set;}
公共字符串值10{get;set;}
公共字符串值11{get;set;}
公共字符串值12{get;set;}
}
公共结构位置键
{
公共字符串值1{get;set;}
公共int值2{get;set;}
公共字符串值3{get;set;}
公共字符串值4{get;set;}
公共字符串值5{get;set;}
公共字符串值6{get;set;}
公共覆盖int GetHashCode()
{
返回值(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);
}
公共覆盖布尔等于(对象对象对象)
{
位置键组合键=(位置键)对象;
返回值1==compositeKey.Value1
&&Value2==compositeKey.Value2
&&Value3==compositeKey.Value3
&&Value4==compositeKey.Value4
&&Value5==compositeKey.Value5
&&Value6==compositeKey.Value6;
}
}
公共结构GroupbyPositionKey
{
公共字符串值1{get;set;}
公共int值2{get;set;}
公共覆盖int GetHashCode()
{
返回值(Value1!=null?Value1.GetHashCode():1)
^(Value2>0?Value2.GetHashCode():1);
}
公共覆盖布尔等于(对象对象对象)
{
位置键组合键=(位置键)对象;
返回值1==compositeKey.Value1
&&Value2==compositeKey.Value2;
}
}
公共课程
{
/*         
值1值2值3值4值5值6值7值8值9值10值11值12
-------------------------------------------------------------------------------------------------
v1 val1 val2 val3 val4 val5 val6 val7 val8 val9 val10
v1 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
分组依据-值1值2值3值4值5值6值7值8值9值10值11值12
根据group by Value1、Value2获取分组后的行列表
*/
公共静态void main()
{
Dictionary dictpositions=新字典();
职位obj=新职位();
obj.Value1=“v1”;
obj.Value2=1;
obj.Value3=“val1”;
obj.Value4=“val2”;
obj.Value5=“val3”;
obj.Value6=“val4”;
obj.Value7=“val5”;
//……等等……等等……对于所有对象。
/*
我有一个如上所述的datatable,我正在用相应的键插入类的collection对象中的行
并将其添加到字典中。
*/
位置键=新位置键();
key.Value1=“v1”;
key.Value2=1;
key.Value3=“val3”;
key.Value4=“val4”;
key.Value5=“val5”;
key.Value6=“val6”;
如果(!dictpositions.ContainsKey(键))
{
dictpositions.Add(key,obj);//此字典将包含原始数据,这些数据将表中的所有行作为字典集合
}
/*
现在,我必须创建另一个字典,该字典通过分组给我一个所有记录的列表
一把钥匙的钥匙
生成的词典应类似于dictionary
*/
字典结果=新字典();
result=dictPositions.GroupBy(..);//不确定如何操作?
}
}
在结果中,我希望有另一个dictionary,其中key对象为value1、value2,value对象为List(Positions)。我不知道如何在字典中找到小组,以得到想要的结果


我已经通过手动循环遍历字典,然后根据新键拾取并插入到另一个字典中,从而实现了这个结果。但是我想知道是否有一种LINQ方法可以做到这一点,它将变得更短。

在您的
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());
字典结果=口述
.GroupBy(x=>newgroupbypositionkey{