【问题标题】:Linq Key that contains only the first of each duplicate仅包含每个重复项中的第一个的 Linq 键
【发布时间】:2017-09-18 19:17:48
【问题描述】:

必须在我的应用程序中将列表转换为字典,但我收到一条错误消息,提示“已添加具有相同键的项目”。 我需要第一把钥匙和他的价值

 Dictionary<string, string> cells = 
             (from cell in sheet.Cells["A1:J20"] 
             where cell.Start.Column == 1  && cell.Value != null
             select sheet.Cells[cell.Start.Row, 1, cell.Start.Row,9].Value)
             .Cast<object[,]>()
             .Distinct().ToDictionary(k => Convert.ToString(k[0, 2]), v => 
             Convert.ToString((v[0, 8])));

Excel 示例:

  • key => 价值

  • Key1 => Value1

  • Key2 => Value2
  • Key3 => Value3
  • Key3 => Value4
  • Key3 => Value5
  • Key6 => Value6
  • 键7 => 值7
  • Key23 => Value8

编辑

 Dictionary<string, string> dict = new Dictionary<string, string>();

                    var cells = (from cell in sheet.Cells["A1:B16"]
                                 where cell.Start.Column == 1 && cell.Value != null
                                 select sheet.Cells[cell.Start.Row, cell.Start.Column, cell.Start.Row, 2].Value)
                                                        .Cast<object[,]>();

循环并添加到字典:

  foreach (var i in cells) {
                        if (dict.ContainsKey(Convert.ToString(i[0, 0])) == false)
                            dict.Add(Convert.ToString(i[0, 0]), Convert.ToString(i[0, 1]));
                        // dict.Distinct();

                    }

但我需要 linq 中的代码!!!

【问题讨论】:

    标签: c# excel linq dictionary


    【解决方案1】:

    警告:未经测试的代码,我无法轻松创建测试环境:

    Dictionary<string, string> cells = 
             (from cell in sheet.Cells["A1:J1"] 
             where cell.Value != null
             let key = sheet.Cells[cell.Start.Row, 3].Value.ToString()
             let val = sheet.Cells[cell.Start.Row, 9].Value.ToString()
             group val by key into vg
             select new { vg.Key, Val = vg.FirstOrDefault() })
             .ToDictionary(k => k.Key, v => v.Val);
    

    另外,优化了很多我觉得有问题的地方。

    【讨论】:

    • 你用什么来让 Linq to sheet.Cells[](Excel.Range 对象?)工作?当我设置 Excel.Interop 时,我无法将 Linq 与 Range 对象一起使用。
    • 我也没有看到包括 Start.Column 或 Start.Row 在内的标准 Excel 互操作?
    • 好的,我想我修复了 group by 语法中的一个错误。还在 Key/Val 上添加了 ToString,因为标准 Interop 似乎返回了很多 Object。
    【解决方案2】:

    我决定我的问题

    Dictionary<string, string> dict = (from cell in sheet.Cells["A1:B34"]
                                     where cell.Start.Column == 1 && cell.Value != null
                                     select sheet.Cells[cell.Start.Row, cell.Start.Column, cell.Start.Row, 2].Value)
                                                            .Cast<object[,]>()
                                                            .GroupBy(k => Convert.ToString(k[0,0]))
                                                            .Select(k =>  k.First())
                                                            .ToDictionary(k => Convert.ToString(k[0, 0]), v => Convert.ToString(v[0, 1]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-11
      • 2021-08-21
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多