【问题标题】:Datatable to dictionary<Int64,List<string>> [closed]数据表到字典<Int64,List<string>> [关闭]
【发布时间】:2012-12-23 02:33:42
【问题描述】:

此 LINQ 表达式不起作用:

dt.AsEnumerable().ToDictionary<Int64, List<string>> (
    dtRow => dtRow.Field<Int64>("CodeVal_1"),
    new List<string> {
        dtRow => dtRow.Field<string>("CodeVal_2"), 
        dtRow => dtRow.Field<string>("CountryCode")
    }
);

dtDataTable,我添加了对DataSetExtensions 的引用。

完整代码

    using (DataSet dsIps = DbConnection.db_Select_Query("use mydb select * from tblCountryCodes"))
    {
        using (DataTable dt = dsIps.Tables[0])
        {
            dt.AsEnumerable().ToDictionary<Int64, List<string>>(
            dtRow => dtRow.Field<Int64>("CodeVal_1"),
            new List<string> {
                    dtRow => dtRow.Field<string>("CodeVal_2"), 
                    dtRow => dtRow.Field<string>("CountryCode")
                }
            );
        }
    }

错误列表

【问题讨论】:

标签: c# linq c#-4.0


【解决方案1】:

根据附加信息,问题在于您没有将正确的参数传递给ToDictionary。它需要两个 lambda,而不是 lambda 和 List&lt;&gt;

这是修复代码的第一步:

dt.AsEnumerable().ToDictionary(
    dtRow => dtRow.Field<Int64>("CodeVal_1"),
    dtRow => new List<string> {
        dtRow.Field<string>("CodeVal_2"), 
        dtRow.Field<string>("CountryCode")
    }
);

编辑:修复了使用错误版本的ToDictionary

【讨论】:

  • 哇,你是怎么找到它的?我知道它没有采用正确的参数。这就是我要问的img69.imageshack.us/img69/6442/6errorsv.png
  • @MonsterMMORPG:你从来没有说过问题在于没有采用正确的参数。你刚才说它不工作。有关完整解决方案,请参阅 Stuart Golodetz 的答案。
  • @MonsterMMORPG:我编辑了我的问题,因为 Stuart Golodetz 删除了他的答案。
  • 我从来没有得到过这么糟糕的答案。你的回答没有任何意义,因为它仍然给出了很多错误。那么这个答案如何?
  • @siride 将 ToDictionary> 更改为 ToDictionary。那应该没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-28
  • 2021-10-08
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
相关资源
最近更新 更多