LINQ到数据集,在获取字符字段时出错
本文关键字:字符 字段 出错 获取 数据集 LINQ | 更新日期: 2023-09-27 18:09:29
var dadproductlist = from dgdproduct in dadData.Tables[0].AsEnumerable()
select new DeliveryAdjustmentProduct
{
AdjustmentQuantity = dgdproduct.Field<int>("AdjustedQty"),
AdjustmentType = dgdproduct.Field<char>("AdjustmentType").ToString(),
DeliveredDate = dgdproduct.Field<DateTime>("ExpectedDate"),
ProductCode = dgdproduct.Field<int>("ProductNum").ToString(),
RevisedQuantity = dgdproduct.Field<int>("RevisedOrderQty"),
SupplierId = dgdproduct.Field<int>("SupplierId").ToString(),
TrailerId = "Pradeep"
};
我已经写了上面的查询来获得一个字符字段,它的失败与"Specified Cast Not Valid"
。
我已经写了上面的查询来获得一个字符字段,它失败了"指定的强制转换无效"
如果你从数据库中获取数据,那么CHAR
在c#中映射到string
。所以尝试:
AdjustmentType = dgdproduct.Field<string>("AdjustmentType"),
代替
AdjustmentType = dgdproduct.Field<char>("AdjustmentType").ToString(),
您可以修改AdjustmentType
的类型,或者尝试从要分配给char
类型AdjustmentType
的字符串中获取第一个字符,如:
AdjustmentType = dgdproduct.Field<string>("AdjustmentType")[0]
(但上面只假设在返回的字符串中至少有一个字符)