2

I am getting one exception while using a substring with lastindex of, exception is given below

LINQ to Entities does not recognize the method 'Int32 LastIndexOf(System.String)' method, and this method cannot be translated into a store expression

my db stored file name format is like this C:\Data\MyFileName.xml and I am passing the name of the file to find out specific record

filename = MyFileName.xml

var record = (from fd in db.Details
where (fd.FullName.Substring(fd.FileName.LastIndexOf("\\") + 1)) == fileName
                          select fd).First();
ekad
  • 14,436
  • 26
  • 44
  • 46
niknowj
  • 977
  • 4
  • 19
  • 37

2 Answers2

2

Unfortunately, LastIndexOf method is not mapped in LINQ to Entities.

But, I think EndsWith can make it for your case:

where fd.FileName.EndsWith(fileName)

or to make it better, concatenate fileName with @"\":

where fd.FileName.EndsWith(@"\" + fileName)
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
0

From my experience, I know that there are certain C# functions which cannot be used in a Linq query, even if they show up in your editor. I believe the reason is that it gets translated to SQL which does not have a way of implementing them. I would try selecting the entire name (the first one) and then doing the substring function after the query.

GuacoIV
  • 65
  • 5