不允许在查询中显式构造实体类型'tblpayment'

时间:2014-03-08 14:21:47

标签: asp.net linq

我使用linq在我的Web应用程序中编写了一个常见查询,但在执行后我得到了这个错误:

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;
    } 

我搜索了这个问题,但我找不到任何有用的解决方案。

谢谢。

1 个答案:

答案 0 :(得分:1)

这里有一个很好的例外解释:

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();
}