gpt4 book ai didi

c# - 如何连接字符串与小数?

转载 作者:行者123 更新时间:2023-11-30 22:05:01 25 4
gpt4 key购买 nike

我在 tbl_BillingTerm 中有 2 列

  1. BTTitle (VARCHAR(MAX))
  2. BTBill(钱)

using (var db = new ClothEntities())
{
var data = (from bterm in db.tbl_BillingTerm
select new
{
bterm.BTId,
BillingTerm = bterm.BTTitle +""+ bterm.BTBill+": USD/month"
}).ToList();
}

我想连接 Title 和 Bill,如我使用 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-to-objects 进行笨拙的字符串操作。

ToList() 之后的所有内容都不会被 EF 提供程序解释。


或者,如果你真的想做这个服务器端,你可以试试,

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")
});
}

这使用了 SQLFunctions EF 提供程序应映射到适当的规范 TSQL 函数的类。

在这种情况下,我认为第二种方法没有什么值(value)。上面的拆分解决方案导致更密集的结果集被传输到客户端。与结果的第二次迭代相比,进程间通信的成本可能要高得多。

关于c# - 如何连接字符串与小数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24670903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com