将列表嵌套在另一个列表中的值上

时间:2015-12-15 14:57:30

标签: c# sql linq list

我正在尝试将嵌套列表添加到另一个列表,其中两个ID都相同。示例如下:

我有一个主列表,其中包含其他表的外键:

列出PayslipMainlst = new List();

        PayslipMainlst.Add(new PayslipMain(0, "Grade 8 (Professional S)", "43", 45954.00, "Default Cost Centre", "TAX REF: 065/L11438", "There is no historic message stored for this payslip.", 1, 1, 1, 1));
        PayslipMainlst.Add(new PayslipMain(1, "Grade 8 (Professional S)", "43", 50000.00, "Default Cost Centre", "TAX REF: 065/L11608", "There is no historic message stored for this payslip.", 2, 2, 2, 2));

最后四个值是外键,下面的示例显示了它们的相关性:

列出Deductionslst = new List();

        /* 28th September 2015 */
        Deductionslst.Add(new Deductions(1, "Tax Paid", "527.00", "3161.80"));
        Deductionslst.Add(new Deductions(1, "Employee NI", "301.72", "1810.32"));

        /*28th October 2015 */
        Deductionslst.Add(new Deductions(2, "Tax Paid", "590.50", "1400.20"));
        Deductionslst.Add(new Deductions(2, "Employee NI", "900.12", "5025.83"));

因此,我们的想法是从PayslipMainlst列表中查询一条记录,相关的表将根据外键添加到嵌套列表中。

有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用GroupBy按相同的属性对项目进行分组:

示例:

var dict = new Dictionary<int,string>
{
   {1,"1"},
   {2,"1"},
   {3,"2"},
   {4,"2"},
   {5,"3"},
};

var grp = dict.GroupBy(kvp => kvp.Value);

结果是三组:

  1. 值为“1”的组,键为:1.2。
  2. 值为“2”的组,键为:3,4。
  3. 值为“3”的组,键为: