【发布时间】:2016-11-05 09:18:43
【问题描述】:
我有一个包含以下列的表格。
CREATE TABLE [dbo].[tbl_QuizQue](
[id] [int] IDENTITY(1,1) NOT NULL,
[question] [nvarchar](max) NOT NULL,
[opt1] [nvarchar](50) NOT NULL,
[opt2] [nvarchar](50) NOT NULL,
[opt3] [nvarchar](50) NOT NULL,
[ans] [nvarchar](50) NOT NULL
)
我正在尝试从该表中获取随机记录,但只有特定列,例如 id、question、opt1、opt2、opt3。目前我正在获取随机记录,但它正在获取所有列。我尝试了下面的代码来获取具有特定列的记录
dbUserEntities obj = new dbUserEntities();
// GET api/values
public IEnumerable<tbl_QuizQue> Get()
{
var result = obj.tbl_QuizQue.OrderBy(r => Guid.NewGuid()).Select(o => new { o.id, o.question, o.opt1, o.opt2, o.opt3 });
return result;
}
在这里,我在return result; 时遇到错误
Cannot implicitly convert type 'System.Linq.IQueryable<AnonymousType#1>' to
'System.Collections.Generic.IEnumerable<studentQuiz.tbl_QuizQue>'. An explicit conversion exists (are you missing a cast?)
帮助我哪里做错了。
【问题讨论】:
-
获取整个实体会更简单。您的列看起来其中之一不够大,不足以证明额外的复杂性。
-
result的值是匿名对象的集合(使用.Select()创建),而不是tbl_QuizQue对象的集合,因此例外。
标签: c# mysql asp.net-mvc entity-framework