c# - 如何在 linq 查询中使用子字符串?

标签 c# linq

我在使用带有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/

相关文章:

c# - 如何在 LINQ 中查询基于链接表的数据

c# - "Value Does not fall in the expected range"ClickOnce部署

c# - 如何将命令中的文本框文本作为参数传递 WPF

c# - 使用自连接在 LINQ 中查询

c# - 对于从函数输出参数接收的变量,使用未分配的参数编译器错误?

c# - 如何在Windows应用商店应用程序中获取图像的一个像素(C#)

c# - Entity Framework 6 Code First 自定义函数

c# - 泛型中 Contains 方法的替代方案

c# - 过滤掉 Linq 中的空值

Linq group by + where 对于每个组