This question already has answers here:
Only parameterless constructors and initializers are supported in LINQ to Entities
(14个回答)
4年前关闭。
因此,当我尝试从Web Api获取数据时会发生这种情况:
ExceptionMessage:
LINQ to Entities仅支持无参数的构造函数和初始化程序。
这是我的服务:
public async Task<ICollection<TicketDto>> GetAllUnansweredTicketsAsync()
{
return await _context.Tickets.Include(m => m.Message)
.Include(u=>u.User)
.OrderByDescending(d=>d.Message.Date)
.Select(t => new TicketDto(t)).ToListAsync();
这是我的DTO的样子:
public class TicketDto
{
public int ID { get; set; }
public string Username { get; set; }
public string Issue { get; set; }
public DateTime Date { get; set; }
public TicketDto(Ticket ticket)
{
ID = ticket.ID;
Username = ticket.User.UserName;
Issue = ticket.Message.Title;
Date = ticket.Message.Date;
}
}
有人可以解释我该如何解决吗?
c#大神给出的解决方案
在Linq to Entities中,您的代码在服务器端执行,但是提供程序无法将构造函数转换为SQL。
您可以解决添加空构造函数并设置属性的问题:
...Select(t => new TicketDto
{
ID = t.ID,
Username = t.User.UserName,
Issue = t.Message.Title,
Date = t.Message.Date,
});
public class TicketDto
{
public TicketDto()
{
}
...
但是,如果您只想保留实际的构造函数,则可以将Linq切换为调用AsEnumerable()
的对象:
...
.AsEnumerable()
.Select(t => new TicketDto(t))
.ToListAsync();
在Java中将华氏度转换为摄氏温度[重复] - javaThis question already has answers here: Double value returns 0 [duplicate] (3个答案) 6年前关闭。 我正在尝试将华氏度转换为摄氏温度。现在当我使用此错误时:double fahrenheit=input.nextDouble(); double celsius= ( **5** /…
从文件中读取用户名和密码[重复] - pythonThis question already has answers here: How to read a file line-by-line into a list? (28个答案) 2年前关闭。 如何在Python中逐行读取文本(例如用户名和密码)?例如,我可以在shell / bash中实现此目的:#!/bin/bash AUTH='/aut…
Python:检查新文件是否在文件夹中[重复] - pythonThis question already has answers here: How do I watch a file for changes? (23个答案) 3年前关闭。 我是python的新手,但是我尝试创建一个自动化过程,其中我的代码将侦听目录中的新文件条目。例如,某人可以手动将zip文件复制到一个特定的文件夹中,并且我希望我的代码能够在文件完全…
python切片的奇怪行为[重复] - pythonThis question already has answers here: Reversing a list slice in python (2个答案) 7个月前关闭。 假设我们有以下列表:>>> a = [x for x in range(10)] >>> print(a) [0, 1, 2, 3, 4, 5, 6…
Flask to Dygraph-如何传递数据? - javascript如果我有一个简单的Python时间数据系列,例如:graphdata = [] graphdata.append( [(datetime.date(2008, 5, 7)),75]) graphdata.append([(datetime.date(2008, 5, 8)), 85]) graphdata.append([(datetime.date(200…