Reputation: 8359
This my method in my repository:
public List<Question> getallquestion()
{
var hej = from Core in db.CoreValue
join Question in db.Question on Core.CID equals question.QID
join Subject in db.SubjectType on Core.CID equals Subject.SID
select new
{
Core.Cname,
Question.QuestionText,
Subject.Sname
};
return hej.ToList();
}
This is the error:
Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'NKI3.Models.Question'
What is the solution for this error? I cant seem to find it
My CreateViewModel:
public class CreateViewModel
{
public string QuestionText { get; set; }
public string Sname { get; set; }
public string Cname {get;set;}
}
}
Thanks in Advance!
Best Regards!
Upvotes: 2
Views: 180
Reputation: 12440
Change your select to read like so:
public List<CreateViewModel> GetAllQuestionViewModel()
{
var hej = from Core in db.CoreValue
join Question in db.Question on Core.CID equals question.QID
join Subject in db.SubjectType on Core.CID equals Subject.SID
select new CreateViewModel
{
Cname = Core.Cname,
QuestionText = Question.QuestionText,
Sname = Subject.Sname
};
return hej.ToList();
}
In your controller you would do something like this:
public ActionResult Index() {
IEnumerable<CreateViewModel> questions = someContext.GetAllQuestionViewModel();
return View(questions);
}
Upvotes: 1
Reputation: 125630
Your linq query produces List of anonymous object (as you use select new { (...) }
without any type). You should replace it with select new Question() { (...) }
. You should check that select syntax depending on Question class properties.
eg. When you have Name
, Text
and Subject
property is should look like this:
var hej = from c in db.CoreValue
join q in db.Question on c.CID equals q.QID
join s in db.SubjectType on c.CID equals s.SID
select new Question
{
QID = q.QID,
QuestionText = q.QuestionText
};
Upvotes: 5
Reputation: 15663
You can't return an anonymous type.
Create a class
public class QuestionResult
{
public string Core {get; set;}
public string Question {get; set;}
public string Subject{get; set;}
}
and use it
public List<QuestionResult> getallquestion()
{
var hej = from Core in db.CoreValue
join Question in db.Question on Core.CID equals question.QID
join Subject in db.SubjectType on Core.CID equals Subject.SID
select new QuestionResult
{
Core = Core.Cname,
Question = Question.QuestionText,
Subject = Subject.Sname
};
return hej.ToList();
}
Upvotes: 0
Reputation: 34349
Your method is expecting to return a collection of your Question
type, but your LINQ query is returning a sequence of an anonymous type. You should change your query, for example changing your projection:
public List<Question> getallquestion()
{
var hej = from Core in db.CoreValue
join Question in db.Question on Core.CID equals question.QID
join Subject in db.SubjectType on Core.CID equals Subject.SID
select new Question
{
Name = Core.Cname,
Text = Question.QuestionText,
Subject = Subject.Sname
};
return hej.ToList();
}
This is using an object initializer to assign the property values of the Question
type from the result of the query. Your property names will differ.
Upvotes: 1