7 R*_*eds 7 c# linq linq-to-entities entity-framework
我有一个表,其中包含一个包含主机名的字符串字段.它们大多是完全合格的域名,但多年来第一个"点"之后的域位已经改变,因为各种DNS变化已经落在我们身上.所以我可能将表中的机器"tom"作为:
tom.company.com
tom.it.company.com
tom.newComapnyBranding.com
...
Run Code Online (Sandbox Code Playgroud)
我经常要对"当前"主机名和这个历史商店进行比较.做类似的事情:
WHERE
UPPER(SUBSTRING(@foo, 1, CHARINDEX(".", @foo))) =
UPPER(SUBSTRING(myDB.myTable.machineName, 1, CHARINDEX(".", myDB.myTable.machineName)))
Run Code Online (Sandbox Code Playgroud)
好吧,我试图将其中一个转换为Linq查询,但我在"索引"部分磕磕绊绊.我差一点接近:
myTable.machineName.ToUpper().Substring(0, myTable.machineName.IndexOf("."))
.Equals(foo.ToUpper().Substring(0, foo.IndexOf(".")))
Run Code Online (Sandbox Code Playgroud)
但是visual studio正在抱怨"IndexOf".它声称我需要将IndexOf更改为:
IndexOf(".", StringComparison.Ordinal))
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我收到异常消息:
LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String,
System.StringComparison)' method, and this method cannot be translated into
a store expression
Run Code Online (Sandbox Code Playgroud)
你如何在Linq中使用这种基于索引的子字符串?
给予实体框架的表达式仅限于可以转换为SQL的表达式.EF不知道如何翻译String.IndexOf成SQL.
你可以改用SqlFunctions.CharIndex:
SqlFunctions.CharIndex(machineName, ".")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1233 次 |
| 最近记录: |