C# 如何将字符串与十进制连接起来?
我在tbl_BillingTerm中有两列C# 如何将字符串与十进制连接起来?,c#,sql,linq,C#,Sql,Linq,我在tbl_BillingTerm中有两列 BTTitle(VARCHAR(最大值)) 钞票(钱) 我想连接标题和账单,如我用LINQ编码的那样。 它正在生成一个错误: > "Unable to cast the type 'System.Decimal' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types." 相反 using (va
我想连接标题和账单,如我用LINQ编码的那样。 它正在生成一个错误:
> "Unable to cast the type 'System.Decimal' to type 'System.Object'.
LINQ to Entities only supports casting EDM primitive or enumeration types."
相反
using (var db = new ClothEntities())
{
var raw = (from bterm in db.tbl_BillingTerm
select new
{
bterm.BTId,
bterm.BTTitle,
bterm.BTBill
}).ToList();
var data = raw.Select(bterm => new
{
bterm.BTId,
BillingTerm = string.Format(
"{0} {1}: USD/month",
bterm.BTTitle,
bterm.BTBill)
});
}
本质上,使用EF获取数据,然后使用linq对对象执行笨拙的字符串操作
EF提供程序不会解释ToList()
之后的所有内容
或者,如果你真的想做这个服务器端,你可以试试
using (var db = new ClothEntities())
{
var data = (from bterm in db.tbl_BillingTerm
select new
{
bterm.BTId,
BillingTerm =
bterm.BTTitle + " " +
SqlFunctions.StringConvert(bterm.BTBill)
+ ": USD/month")
});
}
这将使用EF提供程序应该映射到相应规范TSQL函数的类
在这种情况下,我认为第二种方法没有什么价值。上面的拆分解决方案将导致更密集的结果集传输到客户端。进程间通信的成本可能比结果的第二次迭代要高得多。对于int-to-string转换没有任何限制
如果此错误阻止您继续进行,并且是一个小数据集,您可以通过枚举查询(调用ToList
)从数据库中检索。从那时起,您的操作将针对内存中的对象,并且您可能不会遇到收到的错误
using (var db = new ClothEntities())
{
var result = (from bterm in db.tbl_BillingTerm
select new
{
bterm.BTId,
bterm.BTTitle,
bterm.BTBill
}).ToList();
var data = result.Select(bterm => new
{
bterm.BTId,
BillingTerm = string.Format(
"{0} {1}: USD/month",
bterm.BTTitle,
bterm.BTBill)
});
}
此异常引发的确切位置,异常类型是什么?请尝试显式强制转换:
(string)bterm.BTBill
LINQ to Entities无法识别方法“System.string Format(System.string,System.Object,System.Object)”方法,并且无法将此方法转换为存储表达式。这正是我得到的答案,但6分钟后,我得到了答案,但解释得更多
using (var db = new ClothEntities())
{
var result = (from bterm in db.tbl_BillingTerm
select new
{
bterm.BTId,
bterm.BTTitle,
bterm.BTBill
}).ToList();
var data = result.Select(bterm => new
{
bterm.BTId,
BillingTerm = string.Format(
"{0} {1}: USD/month",
bterm.BTTitle,
bterm.BTBill)
});
}