gpt4 book ai didi

c# - SQL 查询 CAST 到 int + 1 到 LINQ

转载 作者:太空狗 更新时间:2023-10-30 01:29:15 27 4
gpt4 key购买 nike

我正在为以下 linq to sql 查询绞尽脑汁。想法是获得与所有 REG_CODE 项目相比的下一个最小整数。该字段 (REG_CODE) 是一个 varchar(10) 字段。我正在尝试将以下 tsql 转换为 linq-to-entities (EF 6.0):

SELECT TOP 1 CAST( [Extent1].[REG_CODE] AS int) + 1 
FROM [dbo].[Notifications] [Extent1]
WHERE NOT ([Extent1].[REG_CODE] LIKE N'%~[a-z]%' ESCAPE N'~') AND (1 = (ISNUMERIC([Extent1].[REG_CODE]))) AND
NOT EXISTS(SELECT * FROM [dbo].[Notifications] t2 WHERE CAST( t2.REG_CODE AS int) = CAST( [Extent1].[REG_CODE] AS int) + 1 )
ORDER BY [Extent1].[REG_CODE]

(注意 +1 的,我要下一部分)设计并不是那么棒。字段 [REG_CODE] 应该是一个整数字段,但现在不是,也不会很快。

这个:

float notificationMaxRegCodeNumeric =
db.Notifications.Where(not => not.Reg_Code != null && !not.Reg_Code.Contains("[a-z]") && SqlFunctions.IsNumeric(not.Reg_Code) == 1)
.OrderByDescending(not => not.Reg_Code)
.Select(not => not.Reg_Code)
.Cast<int>()
.Max();

成功转换为:

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>();

但它抛出:

DbExpressionBinding requires an input expression with a collection ResultType`.

还有 Convert.ToInt32() 正在抛出:

linq to entities does not recognize the method 'int32 toint32(system.string)' method`

(.Max() 与我正在寻找的内容并不相关,但它在查询的工作部分)

有什么建议吗?

最佳答案

首先,恭喜您找到Cast<T>()诡计!这似乎是唯一开箱即用的 EF6 类型转换方式 string到别的东西 - 所有其他尝试,如 (int)(object)stringValueConvert.ToInt32(stringValue)只是因为不受支持而被阻止。

但请注意 Cast<T>()IEnumerable 定义了方法和 IQueryable结果分别是IEnumerable<T>IQueryable<T> ,即处理序列并产生序列。它出现在string因为stringIEnumerable<char> ,因此 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();

转换为

SELECT TOP (1)
[Project1].[C1] AS [C1]
FROM ( SELECT
CAST( [Extent1].[Reg_Code] AS int) + 1 AS [C1]
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])))
) AS [Project1]
WHERE NOT EXISTS (SELECT
1 AS [C1]
FROM [dbo].[Notifications] AS [Extent2]
WHERE CAST( [Extent2].[Reg_Code] AS int) = [Project1].[C1]
)
ORDER BY [Project1].[C1] DESC

关于c# - SQL 查询 CAST 到 int + 1 到 LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340030/

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