Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在组联接linq查询中将字符串转换为十进制_C#_Sql_Linq_Entity Framework - Fatal编程技术网

C# 在组联接linq查询中将字符串转换为十进制

C# 在组联接linq查询中将字符串转换为十进制,c#,sql,linq,entity-framework,C#,Sql,Linq,Entity Framework,我必须连接两个表,但只返回第二个表中的记录,其中与第一个表中的记录关联的所有记录的“值”之和相同 from p in db.TPs join n in db.TNs on p.Key equals n.Key where (decimal.Parse(p.Value) == db.TNs.Where( nn => nn.Key == p.Key ) .Sum( nn=> decimal.Parse(kk.V

我必须连接两个表,但只返回第二个表中的记录,其中与第一个表中的记录关联的所有记录的“值”之和相同

from p in db.TPs
join n in db.TNs
on p.Key equals n.Key
where (decimal.Parse(p.Value) == db.TNs.Where( nn => nn.Key == p.Key )
                                       .Sum( nn=> decimal.Parse(kk.Value)))
我首先使用实体框架代码

当然,林克抱怨

LINQ to实体无法识别方法“System.Decimal” Parse(System.String)方法

表是巨大的,我必须减少输出,所以在客户端进行这种转换是不可能的。列类型转换也不是一个选项

SQL查询是:

select * from TP as p
join * from TN as n on n.Key = p.Key
where p.Value = (select sum(cast(n.Value as decimal(12,2))) from TN where Key = p.Key)

不幸的是,LINQ to SQL无法创建具有字符串到十进制转换的SQL表达式

如果要执行此操作,必须使用以下命令执行自己的查询:


您可以将
十进制
转换为
字符串
,而不是将
字符串
转换为
字符串
。如果其他方法不起作用,这种方法可能会起作用。

您可以通过创建一些模型定义的函数来实现这一点。请参阅此链接:

具体来说,要添加一些将字符串转换为十进制和字符串转换为int的函数,请执行以下步骤:

以XML格式打开.EDMX文件,以便编辑文本

将自定义转换函数添加到“CSDL内容”部分的“方案”部分

最后,要调用新方法,请执行以下操作:

using (var ctx = new EFTestDBEntities())
{
    var results = from x in ctx.MyTables
                  let TheTotal = ctx.MyTables.Sum(y => ConvertToDecimal(y.Price))
                  select new
                  {
                      ID = x.ID,
                      // the following three values are stored as strings in DB
                      Price = ConvertToDecimal(x.Price),
                      Quantity = ConvertToInt32(x.Quantity),
                      Amount = x.Amount,
                      TheTotal
                  };
}
您的具体示例如下所示:

from p in db.TPs
join n in db.TNs
on p.Key equals n.Key
where (ConvertToDecimal(p.Value) == 
        db.TNs.Where( nn => nn.Key == p.Key ).Sum( nn=> ConvertToDecimal(kk.Value)))

您是否尝试过使用
Convert.ToDecimal
而不是
decimal.Parse
?@YograjGupta-为什么会有帮助?SQL Server也不知道如何转换。因为当您使用Convert.ToDecimal时,它将生成类似SQL的转换(十进制(29,4),[t0].[value]),所以请尝试此方法。也许这个答案会有帮助:@majkinator,它正在工作,为了测试,我在db中使用这个linq
from x。TestStringToDecimals选择新的{x.Id,val=Convert.ToDecimal(x.Value)}
并且它正在生成sql
选择[t0].[id]作为[id],将[TestStringToDecimal]中的[Decimal(29,4),[t0].[Value])转换为[val作为[t0]
对于我来说。Meh…一些框架…这里的问题是失去了便利性。我正在创建匿名结果,而ExecuteStoreQuery需要type@majkinetor,如果您首先使用代码,为什么不首先在代码第一类中将数据库字段生成为小数?它是现有的数据库。您的意思是,如果我仍然设置它,则e first会做自动转换吗?@Majkinator,我不这么认为。我不太熟悉如何在现有数据库的代码中使用EF first,但您可能想看看是否有什么东西可以让您更轻松。我不知道为什么EF团队还没有在框架中实现这一点,而解决方案正在制定中就这么简单。谢谢你的代码。
// NOTE: Change the "EFTestDBModel" namespace to the name of your model
[System.Data.Objects.DataClasses.EdmFunction("EFTestDBModel", "ConvertToInt32")]
public static int ConvertToInt32(string myStr)
{
    throw new NotSupportedException("Direct calls are not supported.");
}

// NOTE: Change the "EFTestDBModel" namespace to the name of your model
[System.Data.Objects.DataClasses.EdmFunction("EFTestDBModel", "ConvertToDecimal")]
public static decimal ConvertToDecimal(string myStr)
{
    throw new NotSupportedException("Direct calls are not supported.");
}
using (var ctx = new EFTestDBEntities())
{
    var results = from x in ctx.MyTables
                  let TheTotal = ctx.MyTables.Sum(y => ConvertToDecimal(y.Price))
                  select new
                  {
                      ID = x.ID,
                      // the following three values are stored as strings in DB
                      Price = ConvertToDecimal(x.Price),
                      Quantity = ConvertToInt32(x.Quantity),
                      Amount = x.Amount,
                      TheTotal
                  };
}
from p in db.TPs
join n in db.TNs
on p.Key equals n.Key
where (ConvertToDecimal(p.Value) == 
        db.TNs.Where( nn => nn.Key == p.Key ).Sum( nn=> ConvertToDecimal(kk.Value)))