0
Dictionary<string, DataRow> dtUsers;
using (DataSet dsmyTable = DbConnection.db_Select_Query
                     ("use myDb select * from myTable"))
{
    dtUsers= dsmyTable .Tables[0].AsEnumerable().
         ToDictionary(row => row.Field<string>("UserId"));
}

好的 UserId 是一smallint列但我想将其转换为字符串。

我应该如何修改上面的 linq ?

4

2 回答 2

6

您不能将 an 强制int转换为字符串,但您可以获得 int 的字符串表示形式:

dtUsers= dsmyTable .Tables[0].AsEnumerable().
         ToDictionary(row => row.Field<short>("UserId").ToString());

但是,您应该仅出于显示目的这样做。通常,您会希望尽可能长时间地保留 int,因为处理 int 而不是从任何地方从字符串解析它要容易得多。

于 2012-12-23T19:04:12.847 回答
1

只是使用ToString()方法。当然,这只是为了展示您的smallint专栏。

dtUsers= dsmyTable .Tables[0].AsEnumerable().
         ToDictionary(row => row.Field<short>("UserId").ToString());
于 2012-12-23T19:06:34.163 回答