简体   繁体   English

在 vb.net 或 C# 中的 LINQ 中使用 indexOf() 函数?

[英]Use indexOf() function in LINQ in vb.net or C#?

I have a table in database that it has a column name.我在数据库中有一个表,它有一个列名。 I have to change all Items name when I read table before showing in Grid .在 Grid 中显示之前读取 table 时,我必须更改所有 Items 名称。 I must to use this LINQ Clause我必须使用这个 LINQ 条款

Dim hospitalList = From l In db_Obj.VWrmz_HospitalCorrection
                       Select hospitalName = l.HOSPITAL_NAME.Substring(0,l.HOSPITAL_NAME.IndexOf("--"))

when i use above code i get this error message :当我使用上面的代码时,我收到此错误消息:

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. LINQ to Entities 无法识别方法 'Int32 IndexOf(System.String, System.StringComparison)' 方法,并且此方法无法转换为存储表达式。

but when I remove IndexOf() its work correctly:但是当我删除IndexOf()它的工作正常:

Dim hospitalList = From l In db_Obj.VWrmz_HospitalCorrection
                   Select hospitalName = l.HOSPITAL_NAME.Substring(10)

How can I do for remove something from my cells like this?我该怎么做才能从我的细胞中去除这样的东西?

Using anonymous perhaps?也许使用匿名?

Dim hospitalList = From l In db_Obj.VWrmz_HospitalCorrection
                       Select New With {.hospitalName = l.HOSPITAL_NAME.Substring(0,l.HOSPITAL_NAME.IndexOf("--"))}

In your error message, the unrecognized method is "Int32.IndexOf", the invoker of "IndexOf" method is "Int32" instead of "String", is there any possible that your HOSPITAL_NAME can be a number ??在您的错误消息中,无法识别的方法是“Int32.IndexOf”,“IndexOf”方法的调用者是“Int32”而不是“String”,您的 HOSPITAL_NAME 是否可能是数字?

If you cannot find the problem, you can try the regular expressions instead of "Substring" method, like this(I am using C#, so... this is the c# code, sorry for that):如果找不到问题,可以尝试使用正则表达式代替“Substring”方法,如下所示(我使用的是 C#,所以...这是 C# 代码,抱歉):

Regex regex = new Regex("--.*");
var hospitalList = From l In db_Obj.VWrmz_HospitalCorrection
                   Select hospitalName = regex.Replace(l.HOSPITAL_NAME, "");

Hope this can Help希望这可以帮助

Here is a C# version for it.这是它的 C# 版本。
LINQ to EF cannot translate IndexOf method to sql syntax. LINQ to EF 无法将IndexOf方法转换为 sql 语法。 You can fetch the data and perform the operation on it later.您可以获取数据并稍后对其执行操作。

from ll in (from l in db_Obj.VWrmz_HospitalCorrection
 select hospitalName = l.HOSPITAL_NAME).ToList()
 select ll.Substring(0, ll.IndexOf("--"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM