Yoda
Yoda

Reputation: 18068

Why is LINQ expression "Not Supported" when converting a Char to String inside of it?

If I make conversion of firstLetterOfLastName to string by adding empty string "" inside of expression I get the following exception when try to convert result to List:

A first chance exception of type 'System.NotSupportedException' occurred in EntityFramework.SqlServer.dll

Code which gives trouble:

 public ActionResult Index(char firstLetterOfLastName = 'A') {
            var queryResult = db.Persons.Where(person => person.LastName.StartsWith(firstLetterOfLastName + ""))
            .OrderBy(person => person.Id);       
            var list = queryResult.ToList(); // EXCEPTION
            return View(list);
        }

However, when I make conversion earlier outside of the LINQ expression like here:

public ActionResult Index(char firstLetterOfLastName = 'A') {
        string flofnAsString = firstLetterOfLastName + "";
        var queryResult = db.Persons.Where(person => person.LastName.StartsWith(flofnAsString + ""))
        .OrderBy(person => person.Id);
        var list = queryResult.ToList();
        return View(list);
    }

there is no problem. Why is so?

Upvotes: 4

Views: 2639

Answers (1)

Servy
Servy

Reputation: 203821

The error tells you exactly why. The query provider doesn't know how to map that operation into SQL. It does know what to do with the result of that operation when you executed it in your application and evaluated it to its value.

Upvotes: 5

Related Questions