英文:
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<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();
}
答案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<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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论