无论我采用哪种方式,在Linq to Sql中对枚举值使用条件运算符时似乎出现了问题。例如
var ret = from listings in db.Listings
select new Listing
{
ID = listings.ID,
//etc
OrderStatus = listings.OrderItems.Count > 0
? listings.OrderItems.First().Order.OrderStatus : OrderStatus.NotCheckedOut
};
System.Data.SqlClient.SqlException: 转换时转换失败 nvarchar值'Charged'到数据类型 INT ..
当我将envar字段保留为nvarchar映射到字符串时,我得到类似的转换错误。如何解决这个问题?
答案 0 :(得分:1)
这有用,也许还有更好的方法吗?
var ret = from listings in db.Listings
let ordS = listings.OrderItems.Count > 0 ?
listings.OrderItems.First().Order.OrderStatus.ToString() : OrderStatus.NotCheckedOut.ToString()
select new Listing
{
ID = listings.ID,
//etc
OrderStatus = (OrderStatus)Enum.Parse(typeof(OrderStatus), ordS)
};