Member is "not supported in LINQ to Entities"

c# entity-framework linq linq-to-entities

Accepted Answer

You can't call, as was indicated in the comments.ToString() in a query using Linq to Entities. Instead, proceed as follows:

var AgeList = new List<string>();
//retrieve as whatever type Age is, no conversion in SQL Server
var AgeListQry = (from d in db.Actors orderby d.Age select d.Age).ToList();
//convert them after the fact, using Linq to Objects
AgeList.AddRange(AgeListQry.Select(a => a.ToString()).Distinct());

EDIT

I noticed your most recent update, which does indicate thatAge is not a column in a database. After then, you must take this action (presumingBirthDate is accurately plotted)

var AgeList = new List<string>();
//retrieve BirthDate from SQL Server and use ToList() to get it to run immediately
var AgeListQry = (from d in db.Actors orderby d.BirthDate select d.BirthDate).ToList();
//convert them after the fact, using Linq to Objects
AgeList.AddRange(AgeListQry.Select(bd => ((int)(DateTime.Now - bd).TotalDays / 365).ToString()).Distinct());

There is nothing for Linq to Entities to map to when you utilize your expressions to create SQL statements.Age property. Instead, you should utilize SQL Server as much as possible (BirthDate ) and then perform the Age translation on your own. If you'd prefer, you could use the following method call in place of the inline code:

AgeList.AddRange(AgeListQry.Select(bd => CalculateAge(bd)).Distinct());
//...
private string CalculateAge(DateTime birthday)
{
   return ((int)(DateTime.Now - bd).TotalDays / 365).ToString();
}
9
5/14/2014 9:56:02 PM

Popular Answer

You don't have theAge in youDB system, and conversion is not possibleLINQ DB request. You have to place theAge either adding a computed column to your table or client-side collection.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow