Click here to Skip to main content
15,880,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
RankrEntities context = new RankrEntities();
            var query = (from EQ in context.tbl_ExamQuestion
                         join Q in context.tbl_Question on EQ.QuestionID equals   
                         Q.QuestionID
                         where EQ.ExamID == 1 && EQ.Status.Equals("A") &&   
                         Q.Status.Equals("A")
                         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);


My result when I call this function by ajax method is as follows :

Object {QuestionID: 1, Question: "The DBMS acts as an interface between what two components of an enterprise-class database system?", obj: "{ QuestionOID = 1, Options = Database application …OID = 4, Options = Database application and SQL }", ans: Array[1]}Question: "The DBMS acts as an interface between what two components of an enterprise-class database system?"



In above query, "options" returns multiple QuestionOID and Options.
Now we are using string.Join to separate above result by "," . This result contains "=" sign which cannot be converted into JSON string. Is there any option to replace string.Join from above query? OR how we can convert above query along with result of "options" ??? Thanks in advance :)
Posted
Updated 21-Jan-15 1:28am
v2

 
Share this answer
 
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
C#
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.
C#
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.
JavaScript
{"QuestionID":1,"Question":"MyQuestion","obj":[{"QuestionOID":1,"Options":"Database application"},{"QuestionOID":2,"Options":"xyz"},{"QuestionOID":3,"Options":"pqr"}],"ans":"1"}

Thanks
Srikant
 
Share this answer
 
If you want to convert Serialize json string to JSON object on client side you can use JSON.Parse(JSONstring) method.

and also refer below link
http://stackoverflow.com/questions/2257117/json-string-to-js-object[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900