Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# TSQL查询转换为int+;1到林克_C#_Entity Framework_Linq_Linq To Entities_Expression Trees - Fatal编程技术网

C# TSQL查询转换为int+;1到林克

C# TSQL查询转换为int+;1到林克,c#,entity-framework,linq,linq-to-entities,expression-trees,C#,Entity Framework,Linq,Linq To Entities,Expression Trees,我正在绞尽脑汁研究下面的linq到sql查询。其思想是与所有注册码项相比,得到最小的下一个整数。该字段(注册码)是一个varchar(10)字段。 我正在尝试将以下tsql转换为linq转换为实体(EF 6.0): (注意+1,我要看下一部分) 这个设计并不是那么令人讨厌。字段[REG_CODE]应该是一个整数字段,但现在不是,将来也不会是 这: 到目前为止,我已经: int nextNotificationMaxRegCodeNumericInt = db.Notifications.Wher

我正在绞尽脑汁研究下面的linq到sql查询。其思想是与所有注册码项相比,得到最小的下一个整数。该字段(注册码)是一个varchar(10)字段。 我正在尝试将以下tsql转换为linq转换为实体(EF 6.0):

(注意+1,我要看下一部分) 这个设计并不是那么令人讨厌。字段[REG_CODE]应该是一个整数字段,但现在不是,将来也不会是

这:

到目前为止,我已经:

int nextNotificationMaxRegCodeNumericInt = db.Notifications.Where(not =>
    not.Reg_Code != null && !not.Reg_Code.Contains("[a-z]") &&
    SqlFunctions.IsNumeric(not.Reg_Code) == 1 &&
    db.Notifications.Any(klainternal => not.Reg_Code.Cast<int>() == klainternal.Reg_Code.Cast<int>())
    )
.OrderByDescending(not => not.Reg_Code)
.Select(not => not.Reg_Code)
.Cast<int>();
int-nextNotificationMaxRegCodeNumericInt=db.Notifications.Where(not=>
not.Reg_Code!=null&&!not.Reg_Code.包含(“[a-z]”)&&
SqlFunctions.IsNumeric(非.Reg_代码)==1&&
db.Notifications.Any(klainternal=>not.Reg\u Code.Cast()==klainternal.Reg\u Code.Cast())
)
.OrderByDescending(非=>非.Reg_代码)
.选择(非=>非.Reg_代码)
.Cast();
但它抛出:

DbExpressionBinding需要一个集合ResultType为“”的输入表达式

另外,
Convert.ToInt32()
正在抛出:

linq to实体无法识别方法“int32到int32(system.string)”方法`

.Max()
与我要查找的内容并不相关,而是在查询的工作部分)


有什么建议吗?

首先,祝贺你找到了
Cast()
技巧!它似乎是将
字符串
转换为其他内容的唯一现成的EF6方法-所有其他尝试,如
(int)(object)stringValue
转换.ToInt32(stringValue)
都被简单地阻止为不受支持

但请注意,
Cast()
方法是为
IEnumerable
IQueryable
定义的,结果分别是
IEnumerable
IQueryable
,即处理序列并生成序列。它出现在
string
上,因为
string
IEnumerable
,因此
IEnumerable
,但这不是我们需要的

因此,诀窍是始终使用投影(
Select
)+
Cast
进行转换。将其应用于查询会导致如下结果:

int nextNotificationMaxRegCodeNumericInt = db.Notifications
    .Where(n => n.Reg_Code != null &&
        !n.Reg_Code.Contains("[a-z]") &&
        SqlFunctions.IsNumeric(n.Reg_Code) == 1)
    .Select(n => n.Reg_Code).Cast<int>() // <--
    .Select(reg_Code => reg_Code + 1)
    .Where(reg_Code => !db.Notifications.Select(n => n.Reg_Code).Cast<int>() // <--
        .Contains(reg_Code))
    .OrderByDescending(reg_Code => reg_Code)
    .FirstOrDefault();

我认为不需要用c#将字段强制转换为整数,因为它应该已经这样定义了,no?
cast
是一个Linq方法,它将强制转换集合中的所有值,而不是单个值(我怀疑即使使用正确,它也会转换为SQL)。您需要一个设置为转换为所需SQL的方法。我不相信有什么内置的,但看看这个关于如何创建您自己的@user1666620的问题。字段REG_代码是一个varchar(10)字段,所以我需要最小的下一个整数,与所有REG_代码项相比,ORDER BY REG_CODE当您尝试进行数字排序时,SQL不危险吗,或者
REG\u code
是否包含前导零?能否使用
Convert.ToInt32
显示代码?这是LINQtoEntities的正确方法-您使用的是什么版本的EF?你在用EF Core吗?这就是我要找的!还有一件事是:ORDER BY[Project1].[C1]DESC,这必须没有DESC,然后它会得到下一个不存在的最大注册码。谢谢
SELECT MAX(CAST( [Extent1].[REG_CODE] AS int)) AS[A1]
FROM[dbo].[Notifications] AS[Extent1]
WHERE ([Extent1].[REG_CODE] IS NOT NULL) AND(NOT([Extent1].[REG_CODE] LIKE N'%~[a-z]%' ESCAPE N'~')) AND(1 = (ISNUMERIC([Extent1].[REG_CODE])))
int nextNotificationMaxRegCodeNumericInt = db.Notifications.Where(not =>
    not.Reg_Code != null && !not.Reg_Code.Contains("[a-z]") &&
    SqlFunctions.IsNumeric(not.Reg_Code) == 1 &&
    db.Notifications.Any(klainternal => not.Reg_Code.Cast<int>() == klainternal.Reg_Code.Cast<int>())
    )
.OrderByDescending(not => not.Reg_Code)
.Select(not => not.Reg_Code)
.Cast<int>();
int nextNotificationMaxRegCodeNumericInt = db.Notifications
    .Where(n => n.Reg_Code != null &&
        !n.Reg_Code.Contains("[a-z]") &&
        SqlFunctions.IsNumeric(n.Reg_Code) == 1)
    .Select(n => n.Reg_Code).Cast<int>() // <--
    .Select(reg_Code => reg_Code + 1)
    .Where(reg_Code => !db.Notifications.Select(n => n.Reg_Code).Cast<int>() // <--
        .Contains(reg_Code))
    .OrderByDescending(reg_Code => reg_Code)
    .FirstOrDefault();