-9

この 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")
    }
);

dtは でありDataTable、 への参照を追加しました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")
                }
            );
        }
    }

エラーリストここに画像の説明を入力

4

1 に答える 1

4

追加情報に基づくと、問題は に正しい引数を渡していないことですToDictionary。ラムダと ではなく、2 つのラムダが必要List<>です。

コードを修正するための最初のステップは次のとおりです。

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

編集: の間違ったバージョンを使用して修正しましたToDictionary

于 2012-12-23T02:49:48.427 に答える