简体   繁体   English

[System.DateTime])'没有支持的SQL转换

[英][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 : 在这个查询中,我使用一个函数将我的Datetime转换为另一个演示文稿我使用此函数:

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: 这个函数只是转换我的DateTime,但是当我执行查询时出现了这个错误:

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. 它试图将查询转换为SQL,但不知道如何转换ConvertToPersianToShow方法。

The solution is to call ToList() after the where clause to bring the entities into memory, then do the select: 解决方案是在where子句之后调用ToList()将实体带入内存,然后执行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 调用ToList() ,查询将被转换为SQL,例如

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. 当数据返回并且您将其存储在内存中时,您可以使用LINQ to Objects来进一步查询和操作数据。

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. 通常,在添加至少一个where子句之前,应该小心调用ToList ,否则最终会将太多数据提取到内存中。

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 reference: 方法x没有支持的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. LINQ to SQL不知道如何将调用方法'ConvertToPersianToShow'转换为SQL以便在服务器上执行where子句。 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). 正如有些人所说,你不能在SQL中运行C#(好吧......让我们忽略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. 您的数据层正在执行显示逻辑,并且没有一个Microsoft工程师期望这样做。

You should bring your data out of the database first. 您应该首先将数据从数据库中删除。 Then on your display logic use ConvertToPersianToShow(DateTime?) to bind to your view. 然后在您的显示逻辑上使用ConvertToPersianToShow(DateTime?)绑定到您的视图。

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. Scrap ConvertToPersianToShow ,学习日期时间格式的DateTime.ToString(string)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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