简体   繁体   中英

[System.DateTime])' has no supported translation to SQL

I want to execute a query like this:

List<supervisorAnswerQuesttionPres> temp =
    (from i in dbconnect.tblAnswerLists
    where i.StudentNum == studentNumber
    select new supervisorAnswerQuesttionPres
    {
        answerList = _resAnswerList,
        questionList = _resQuestionist,
        date = ConvertToPersianToShow(i.dateOfAnswer.Value.Date)
    }).OrderBy(i => i.date).ToList();

My class that this query is returned is something like this :

public class supervisorAnswerQuesttionPres
{
    public string date { set; get; }
    public List<string> questionList { set; get; }
    public List<string> answerList { set; get; }
}

In this query i use a function to convert my Datetime to another presentation i use this function for this :

public string ConvertToPersianToShow(DateTime? datetime)
{
    string date;
    DateTime dt;

    if (!datetime.HasValue) return "";
    dt = datetime.Value;
    //  dt = datetime;

    string year = Convert.ToString(persian_date.GetYear(dt));
    string month = Convert.ToString(persian_date.GetMonth(dt));
    string day = Convert.ToString(persian_date.GetDayOfMonth(dt));

    if (month.Length == 1)
    {
        month = "0" + Convert.ToString(persian_date.GetMonth(dt));
    }

    if (day.Length == 1)
    {
        day = "0" + Convert.ToString(persian_date.GetDayOfMonth(dt));
    }

    Convert.ToString(persian_date.GetMonth(dt)) + "/" +
            +                 dt.Minute + ")";
    date = year + "/" + month + "/" + day;
    return date;
}

This function just convert my DateTime ,But when i execute the query i got this error:

Method 'System.String ConvertToPersianToShow(System.Nullable`1[System.DateTime])' has no supported translation to SQL. 

It's trying to convert the query into SQL, but doesn't know how to convert the ConvertToPersianToShow method.

The solution is to call ToList() after the where clause to bring the entities into memory, then do the select:

var temp = dbconnect.tblAnswerLists
    .Where(i => i.StudentNum == studentNumber)
    .ToList() // <-- This will bring the data into memory.
    .Select(i => new supervisorAnswerQuesttionPres
        {
            answerList = _resAnswerList,
            questionList = _resQuestionist,
            date = ConvertToPersianToShow(i.dateOfAnswer.Value.Date)
        })
    .OrderBy(i => i.date)
    .ToList()

When calling ToList() , the query is translated into SQL, eg something like

SELECT * FROM <table> WHERE StudentNum = '<studentNumber>'

and executed against the database. When the data returns and you have it in memory, you can use LINQ to Objects to query and manipulate the data further.

NOTE! Generally you should be careful to call ToList before you've added at least a where clause, otherwise you'll end up fetching way too much data into memory.

try this:

 var temp = (from i in dbconnect.tblAnswerLists
                    let pDate = ConvertToPersianToShow(i.dateOfAnswer.Value.Date)
                    where i.StudentNum == studentNumber

                    select new PresentClass.supervisorAnswerQuesttionPres
                    {
                        answerList = _resAnswerList,
                        questionList = _resQuestionist,
                        date = pDate
                    }).OrderBy(i => i.date).ToList();

reference: Method x has no supported translation to SQL

LINQ to SQL does not know how to translate a call your method 'ConvertToPersianToShow' into SQL in order to execute the where clause on the server. Your method does not exist on the server.

maybe something like this would help, but if it doesn't work you should fetch your data and then change it to the way you want it to be shown as

List<PresentClass.supervisorAnswerQuesttionPres> temp 
= (from i in dbconnect.tblAnswerLists
   let PDate=ConvertToPersianToShow(i.dateOfAnswer.Value.Date)
   where i.StudentNum == studentNumber
   select new PresentClass.supervisorAnswerQuesttionPres
   {
    answerList = _resAnswerList,
    questionList = _resQuestionist,
    date = PDate
   }).OrderBy(i=>i.date).ToList();

As some people have noted, you can't run C# in SQL (well...lets ignore SQL CLR).

However your real problem comes from your poorly architected program.

Your data layer is doing display logic, and none of the Microsoft engineers expected that.

You should bring your data out of the database first. Then on your display logic use ConvertToPersianToShow(DateTime?) to bind to your view.

public class SupervisorAnswerQuestion
{
    public DateTime? Date { set; get; }
    public List<string> Questions { set; get; }
    public List<string> Answers { set; get; }
}

public class SupervisorAnswerQuestionViewModel
{
    public SupervisorAnswerQuestion SupervisorAnswerQuestion {get;set;}
    public string DateFormated 
    {
        get { return SupervisorAnswerQuestion.Date.ToString("yyyy/MM/dd");
    }
}

Actual come to think of it. Scrap ConvertToPersianToShow , learn DateTime.ToString(string) for datetime formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM