我在使用带有lastindex的子字符串时遇到一个异常, 下面给出了异常(exception)情况
LINQ to Entities does not recognize the method 'Int32 LastIndexOf(System.String)' method, and this method cannot be translated into a store expression
我的数据库存储文件名格式是这样的 C:\Data\MyFileName.xml 我正在传递文件名来查找特定记录
filename = MyFileName.xml
var record = (from fd in db.Details
where (fd.FullName.Substring(fd.FileName.LastIndexOf("\\") + 1)) == fileName
select fd).First();
最佳答案
不幸的是,LastIndexOf
method is not mapped in LINQ to Entities 。
但是,我认为 EndsWith
可以满足您的情况:
where fd.FileName.EndsWith(fileName)
或者为了让它更好,将 fileName
与 @"\"
连接起来:
where fd.FileName.EndsWith(@"\" + fileName)
关于c# - 如何在 linq 查询中使用子字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21746482/