我有这段代码:
IDictionary<string, IEnumerable<object>> setValues = new Dictionary<string, IEnumerable<object>>()
{
{ "IN_PARTENER", dtResult.Rows.Cast<DataRow>().Select(row => row["IN_PARTENER"]) },
{ "IN_DATA", dtResult.Rows.Cast<DataRow>().Select(row => row["IN_DATA"]) },
{ "IN_VALOARE", dtResult.Rows.Cast<DataRow>().Select(row => row["IN_VALOARE"]) },
{ "OUT_PARTENER", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_PARTENER"]) },
{ "OUT_DATA", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_DATA"]) },
{ "OUT_VALOARE", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_VALOARE"]) },
{ "TOTAL_ZI", dtResult.Rows.Cast<DataRow>().Select(row => row["TOTAL_ZI"]) },
};
我想将字符串IN_VALOARE
和OUT_VALOARE
从string
转换为decimal
,但我不知道该怎么做
答案 0 :(得分:4)
您可以在Select
:
.Select(row => (decimal)row["IN_VALOARE"])
完整代码:
IDictionary<string, IEnumerable<object>> setValues = new Dictionary<string, IEnumerable<object>>()
{
{ "IN_PARTENER", dtResult.Rows.Cast<DataRow>().Select(row => row["IN_PARTENER"]) },
{ "IN_DATA", dtResult.Rows.Cast<DataRow>().Select(row => row["IN_DATA"]) },
{ "IN_VALOARE", dtResult.Rows.Cast<DataRow>().Select(row => (decimal)row["IN_VALOARE"]) },
{ "OUT_PARTENER", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_PARTENER"]) },
{ "OUT_DATA", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_DATA"]) },
{ "OUT_VALOARE", dtResult.Rows.Cast<DataRow>().Select(row => (decimal)row["OUT_VALOARE"]) },
{ "TOTAL_ZI", dtResult.Rows.Cast<DataRow>().Select(row => row["TOTAL_ZI"]) },
};
由于你不能用不同的值类型创建一个字典(没有得到hacky),或许你应该看一下使用自定义类:
public class MyThing
{
public string InPartener { get; set; }
public string InData { get; set; }
public decimal InValoare { get; set; }
public string OutPartener { get; set; }
public string OutData { get; set; }
public decimal OutValoare { get; set; }
public string TotalZi { get; set; }
}
列出这些课程:
var myThings = dtResult.Rows.Cast<DataRow>().Select(r => new MyThing
{
InPartener = r["IN_PARTENER"],
InData = r["IN_DATA"],
InValoare = (decimal)r["IN_VALOARE"],
OutPartener = r["OUT_PARTENER"],
OutData = r["OUT_DATA"],
OutValoare = (decimal)r["OUT_VALOARE"],
TotalZi = r["TOTAL_ZI"]
};
答案 1 :(得分:1)
您可以在decimal.Parse
部分
IN_VALOARE
{ "IN_VALOARE", dtResult.Rows.Cast<DataRow>().Select(row => decimal.Parse(row["IN_VALOARE"].ToString())) },
给出了
IDictionary<string, IEnumerable<object>> setValues = new Dictionary<string, IEnumerable<object>>()
{
{ "IN_PARTENER", dtResult.Rows.Cast<DataRow>().Select(row => row["IN_PARTENER"]) },
{ "IN_DATA", dtResult.Rows.Cast<DataRow>().Select(row => row["IN_DATA"]) },
{ "IN_VALOARE", dtResult.Rows.Cast<DataRow>().Select(row => decimal.Parse(row["IN_VALOARE"].ToString())) },
{ "OUT_PARTENER", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_PARTENER"]) },
{ "OUT_DATA", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_DATA"]) },
{ "OUT_VALOARE", dtResult.Rows.Cast<DataRow>().Select(row => row["OUT_VALOARE"]) },
{ "TOTAL_ZI", dtResult.Rows.Cast<DataRow>().Select(row => row["TOTAL_ZI"]) },
};
答案 2 :(得分:0)
首先,如果需要IEnumerable<object>
,那么您实际上并不需要将字段值转换为十进制或其他类型。
其次,如果您需要获取字典中的所有列,那么您的所有代码都可以简化为:
var setValues = dtResult.Columns.Cast<DataColumn>()
.ToDictionary(c => c.ColumnName,
c => dtResult.AsEnumerable().Select(r => r.Field<object>(c)));
简单 - 获取所有列,然后创建字典,列名为键,列值集合为值。
注意:这取决于您的需求,但也考虑使用强类型实体而不是字典,字符串和对象。即定义实体
public class Transaction // chose good name here
{
public string InPartener { get; set; }
public string InData { get; set; }
public decimal InValoare { get; set; }
public string OutParener { get; set; }
public string OutData { get; set; }
public decimal OutValoare { get; set; }
public string TotalZi { get; set; }
}
使用LINQ to DataSet将DataTable转换为强类型对象的集合:
var transactions = from r in dtResult.AsEnumerable()
select new Transaction {
InPartener = r.Field<string>("IN_PARTENER"),
InData = r.Field<string>("IN_DATA"),
InValoare = r.Field<decimal>("IN_VALOARE"),
OutPartener = r.Field<string>("OUT_PARTENER"),
OutData = r.Field<string>("OUT_DATA"),
OutValoare = r.Field<decimal>("OUT_VALOARE"),
TotalZi = r.Field<string>("TOTAL_ZI")
};
这将为您提供具有内部意义支持的强大类型的商业模式。例如。如果你需要计算总InValoare:
decimal total = transactions.Sum(t => t.InValoare);