C# Linq 表达式中的 Int.Parse
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12118258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Int.Parse in Linq Expression
提问by Pouya
I have the following LINQ expression. I want calculate the sum of numeric values in an nvarcharfield. I'm using following code to do this, but I get an error when I try to run this.
我有以下 LINQ 表达式。我想计算一个nvarchar字段中数值的总和。我正在使用以下代码来执行此操作,但是当我尝试运行此代码时出现错误。
var m = new MaterialModelContainer();
var list =
(from x in
(
from inv in m.INVs
join l in m.LIBs on inv.MESC equals l.MESC
join o in m.OUTs on inv.MESC equals o.MESC
join t in m.TRANs on inv.MESC equals t.MESC
where t.TYPE == "60"
select new
{
l.MESC,
l.LINE_NO,
l.UNIT_LINE,
Description = l.DES + " " + l.PART_NO,
inv.NEW_QTY,
o.PJ,
o.DATE,
o.QTY,
o.QTY_REC,
TranQty = t.QTY,
tranDate = t.DATE
}
)
group x by
new
{
x.MESC,
x.LINE_NO,
x.UNIT_LINE,
x.Description,
x.NEW_QTY,
x.PJ,
x.DATE,
x.QTY,
x.QTY_REC
}
into g
select new
{
QTY_Consum_1 = g.Where(c => int.Parse(c.tranDate) >= cuDate && int.Parse(c.tranDate) <= endDate).Sum(d => int.Parse(d.TranQty))
}
).ToList();
Error Description:
错误说明:
LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
LINQ to Entities 无法识别方法 'Int32 Parse(System.String)' 方法,并且此方法无法转换为存储表达式
How can I solve this problem and write this code better than this?
我怎样才能解决这个问题并写出比这更好的代码?
I changed the code to this:
我把代码改成这样:
select new
{
QTY_Consum_1 = g.Where(c => SqlFunctions.StringConvert(c.tranDate) >= cuDate && SqlFunctions.StringConvert(c.tranDate) <= endDate).Sum(d => SqlFunctions.StringConvert(d.TranQty)),
g.Key.MESC
}
).ToList();
but got this error:

但得到这个错误:

采纳答案by Pouya
You can't use int.parse in where. You can rewrite your query like this:
你不能在 .parse 中使用 int.parse where。您可以像这样重写查询:
var list = (from x in
(
from inv in m.INVs
join l in m.LIBs on inv.MESC equals l.MESC
join o in m.OUTs on inv.MESC equals o.MESC
join t in m.TRANs on inv.MESC equals t.MESC
where t.TYPE == "60" && t.QTY!=""
select new
{
l.MESC,
l.LINE_NO,
l.UNIT_LINE,
Description = l.DES + " " + l.PART_NO,
inv.NEW_QTY,
o.PJ,
o.DATE,
o.QTY,
o.QTY_REC,
TranQty = t.QTY,
tranDate = t.DATE
}
).ToList()
group x by
new
{
x.MESC,
x.LINE_NO,
x.UNIT_LINE,
x.Description,
x.NEW_QTY,
x.PJ,
x.DATE,
x.QTY,
x.QTY_REC
}
into g
select new
{
QTY_Consum_1 = g.Where(c => int.Parse(c.tranDate) >= cuDate && int.Parse(c.tranDate) <= endDate).Sum(d => int.Parse(d.TranQty)),
g.Key.MESC
}
).ToList();
Call .ToList()method, then use int.Parse(variable).
调用.ToList()方法,然后使用int.Parse(variable).
Have a nice day.
祝你今天过得愉快。
回答by Daniel A. White
In your where clause, you can't call int.Parse. Entity Framework doesn't know how to convert that to SQL. Consider revising your Where.
在您的 where 子句中,您不能调用int.Parse. 实体框架不知道如何将其转换为 SQL。考虑修改您的Where.
回答by Alexandre Jobin
Replace all your int.Parseby SqlFunctions.StringConvert(variable). There's no function to convert String to Int. You should try to do the inverse and convert your Int to String with StringConvert.
替换你所有int.Parse的SqlFunctions.StringConvert(variable). 没有将 String 转换为 Int 的函数。您应该尝试进行相反的操作,并使用 StringConvert 将您的 Int 转换为 String。
The SqlFunctions utilities will be able to translate the command in SQL command.
SqlFunctions 实用程序将能够翻译 SQL 命令中的命令。
回答by Pablo Romeo
Entity Framework can't translate that type of conversion to SQL.
实体框架无法将这种类型的转换转换为 SQL。
Is there any chance you could alter your data structure to use proper data types such as actual DateTimetypes? For large data volumes conversions like that will affect performance.
您是否有机会更改数据结构以使用正确的数据类型,例如实际DateTime类型?对于大数据量,这样的转换会影响性能。
I would recommend either changing your data model types to avoid these conversions, or if the amount of data will always be small, then get the data first, and later use Linq to Objects.
我建议您更改数据模型类型以避免这些转换,或者如果数据量总是很小,那么首先获取数据,然后使用 Linq to Objects。
回答by Yasin Kilicdere
There is a badly built database, which I cannot edit, but have to use it.
有一个糟糕的数据库,我无法编辑,但必须使用它。
I use this in Linq-To-Sql and it works.
我在 Linq-To-Sql 中使用它并且它有效。
First cast the string to object, then cast it to int.
首先将字符串转换为 object,然后将其转换为 int。
from s in db.Students
select new
{
s.Name,
s.Surname,
Birthday = new DateTime((int)(object)(s.dateStr.Substring(0, 4)),
(int)(object)(s.dateStr.Substring(4, 2)),
(int)(object)(s.dateStr.Substring(6, 2))),
}
回答by Mahmoodvcs
EF 5:
英孚 5:
Instead of int.Pasrseuse Convert.ToInt32. Entity Framework will generate proper CASTfunctions in SQL.
而不是int.Pasrse使用Convert.ToInt32. 实体框架将CAST在 SQL 中生成适当的函数。
EF 6:
英孚 6:
Short answer:
简短的回答:
youEntity.Where(c=>SqlFunctions.StringConvert((decimal?)c.INTFIELD).Trim() == STRINGVALUE)
Long answer:
长答案:
in EF 6 you have to convert numeric value to string with SqlFunctions.StringConvert. but it has a problem. It will add unnecessary spaces to the result. so the comparison will fail. That's why I have put Trim()there. I have tested it with EF 6.1.1.
在 EF 6 中,您必须使用SqlFunctions.StringConvert将数值转换为字符串。但它有一个问题。它将在结果中添加不必要的空格。所以比较会失败。这就是为什么我把Trim()它放在那里。我已经用 EF 6.1.1 对其进行了测试。
回答by Alexander Christov
When you need to make sure a CLR method can be used in LINQ to Entities you'd better consult this source: https://msdn.microsoft.com/en-us/library/vstudio/dd456828(v=vs.100).aspx.
当您需要确保可以在 LINQ to Entities 中使用 CLR 方法时,您最好参考此来源:https: //msdn.microsoft.com/en-us/library/vstudio/dd456828(v=vs.100) .aspx。
Not a fast reading, it'd take time, but here's the answer to your question (and many more like it).
不是快速阅读,需要时间,但这是您问题的答案(还有更多类似的问题)。

