Hi,
You can remove the join part and still be able to convert it to javascript array containing JSON.
I have tried to reduce your code
select new
{
Q.QuestionID,
Q.Question,
options = from QO in context.tbl_Question_Option where
QO.Status.Equals("A") && QO.QuestionID ==
Q.QuestionID orderby QO.QuestionOrder select
new { QO.QuestionOID, QO.Options },
ans = from QO in context.tbl_Question_Option where
QO.Status.Equals("A") && QO.QuestionID ==
Q.QuestionID && QO.IsCorrect.Equals("Y") select
QO.QuestionOID,
}).ToList().Select(x => new { x.QuestionID, x.Question, obj =
(string.Join(" ,", x.options)).AsEnumerable(),x.ans });
var ser = new JavaScriptSerializer();
string temp = ser.Serialize(query);
as below. here I have hardcoded some stuff to reduce complexity.
List<Question> list = new List<Question>();
list.Add(new Question() { QuestionOID = 1, Options = "Database application" });
list.Add(new Question() { QuestionOID = 2, Options = "xyz" });
list.Add(new Question() { QuestionOID = 3, Options = "pqr" });
var x = new { QuestionID = 1, Question = "MyQuestion", option = list.Select(QO => new {QO.QuestionOID, QO.Options }), ans = "1" };
var filteredQuery = new { x.QuestionID, x.Question, obj = x.option, x.ans };
var ser = new JavaScriptSerializer();
string temp = ser.Serialize(filteredQuery);
Console.WriteLine(temp);
And the result will look like below.
{"QuestionID":1,"Question":"MyQuestion","obj":[{"QuestionOID":1,"Options":"Database application"},{"QuestionOID":2,"Options":"xyz"},{"QuestionOID":3,"Options":"pqr"}],"ans":"1"}
Thanks
Srikant