我在我的web应用程序中使用linq编写了一个常见的查询,但在执行后我得到了这个错误:
Explicit construction of entity type 'AccidentCongress.dblinqtoDb.tblpayment' in query is not allowed.
我的查询是:
public List<tblpayment> returnpay(string uname)
{
List<tblpayment> q = (from i in db.tblpayments
where i.ownerUsername == uname
select new tblpayment
{
id = i.id
}).ToList();
return q;
}
我搜索了这个问题,但没有找到任何有用的解决方法。
这里有一个很好的异常解释:
https://stackoverflow.com/a/2953058/59849你确定你需要在查询中构造一个新的tblpayment
实例吗?你能不能这样做:
public List<tblpayment> returnpay(string uname)
{
List<tblpayment> q = (from i in db.tblpayments
where i.ownerUsername == uname
select i).ToList();
return q;
}
如果你真的需要根据你的查询创建一个新的tblpayment
对象列表,你可以这样做:
public List<tblpayment> returnpay(string uname)
{
List<tblpayment> q = (from i in db.tblpayments
where i.ownerUsername == uname
select i).ToList();
return q.Select(x => new tblpayment { id = i.id }).ToList();
}