导出日期时间列表为字符串。

huangapple go评论76阅读模式
英文:

export a list of datetimes date as string

问题

以下是翻译好的内容:

我想导出一个包含日期部分为字符串的列表,但出现了以下错误:
System.NotSupportedException: 'LINQ to Entities 不识别 'System.String ToShortDateString()' 方法,而且该方法无法被转换成存储表达式。'

代码如下:

public List<string> ReadDatesDAL(string d)
{
    var q = db.visits.Include("Doctor")
        .Where(i => i.delstatus == false)
        .Where(i => i.doctor.name == d)
        .Select(i => i.start.ToShortDateString());
    return q.ToList();
}

请注意,这是对您提供的代码的翻译。如果您有其他问题或需要进一步的帮助,请随时提出。

英文:

I want to export a list which contains datimes date part as string but the below error ocuured :
System.NotSupportedException: 'LINQ to Entities does not recognize the method 'System.String ToShortDateString()' method, and this method cannot be translated into a store expression.'

the code is:

public List&lt;string&gt; ReadDatesDAL(string d)
{
    var q = db.visits.Include(&quot;Doctor&quot;)
        .Where(i =&gt; i.delstatus == false)
        .Where(i =&gt; i.doctor.name == d)
        .Select(i =&gt; i.start.ToShortDateString());
    return q.ToList();
}

答案1

得分: 1

错误提示为“ToShortString() 无法转换为存储表达式”。更改 Select 以仅返回 i.start。然后可以调用 ToList() 运行查询,然后在之后调用 ToShortString()

public List<string> ReadDatesDAL(string d)
{
    var q = db.visits.Include("Doctor")
        .Where(i => i.delstatus == false)
        .Where(i => i.doctor.name == d)
        .Select(i => i.start);

    return q.ToList().Select(i => i.ToShortDateString()).ToList();
}
英文:

The error states that ToShortString() "cannot be translated into a store expression". Change the Select so that it just returns i.start. You can then call ToList() to run the query and then call ToShortString() afterwards.

public List&lt;string&gt; ReadDatesDAL(string d)
{
    var q = db.visits.Include(&quot;Doctor&quot;)
        .Where(i =&gt; i.delstatus == false)
        .Where(i =&gt; i.doctor.name == d)
        .Select(i =&gt; i.start);

    return q.ToList().Select(i =&gt; i.ToShortDateString()).ToList();
}

huangapple
  • 本文由 发表于 2023年7月6日 19:12:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76628214.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定