/// <summary>
        /// The data from Stackapp can  change everytime we request it so separate this logic out to its own routine
        /// that can be called when we want new data.
        /// </summary>
        private void LoadQuestions()
        {
            DateTime startDate = DateTime.Now.AddDays(-1);
            DateTime endDate   = DateTime.Now;

            Task <StackExchange.StacMan.StacManResponse <Question> > mainQueryQuestiontask = stackAppClient.Search.GetMatchesAdvanced("stackoverflow.com", "withbody", null, null, startDate, endDate, null, null, null, null, null, null, null, true, 2);

            StacManResponse <Question> listOfQuestions = mainQueryQuestiontask.Result;

            List <int> listOfQuestionIds = new List <int>();

            foreach (Question q in listOfQuestions.Data.Items)
            {
                listOfQuestionIds.Add(q.QuestionId);
            }

            // now we have a list of questions with an accepted answer and more than one answer.
            // for each question
            // get the question title and question body  .. this will be one row.

            // get the question title and bodies and accepted answer id associated with list of question ids.
            Task <StackExchange.StacMan.StacManResponse <Question> > questionstask = stackAppClient.Questions.GetByIds("stackoverflow.com", listOfQuestionIds, "withbody");

            listOfQuestionInfo = questionstask.Result;

            BindWithListView();
        }
        /// <summary>
        /// the user has click a view answer. now get the quesiton id form the command argument and
        /// fetch the question and display it above the answer table
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnViewAnswers_Command(object sender, CommandEventArgs e)
        {
            StackOverFlowAnswersListView.Visible = true;

            lblSelectedQuestionsLabel.Visible = true;

            DataTable tableForAnswersListview = new DataTable();

            tableForAnswersListview.Columns.Add("Answer", System.Type.GetType("System.String"));
            tableForAnswersListview.Columns.Add("AcceptedAnswer", System.Type.GetType("System.String"));
            tableForAnswersListview.Columns.Add("QuestionId", System.Type.GetType("System.String"));
            tableForAnswersListview.Columns.Add("AnswerId", System.Type.GetType("System.String"));

            List <Int32> listOfQuestionIds = new List <int>();

            listOfQuestionIds.Add(Int32.Parse(e.CommandArgument.ToString()));

            Task <StackExchange.StacMan.StacManResponse <Question> > selectedQuestionTask = stackAppClient.Questions.GetByIds("stackoverflow.com", listOfQuestionIds, "withbody");

            StackExchange.StacMan.StacManResponse <Question> selectedQuestion = selectedQuestionTask.Result;
            lblSelectedQuestions.Text = selectedQuestion.Data.Items[0].Title;

            Task <StackExchange.StacMan.StacManResponse <Answer> >   answerstask         = stackAppClient.Questions.GetAnswers("stackoverflow.com", listOfQuestionIds, "withbody");
            Action <StackExchange.StacMan.StacManResponse <Answer> > listAnswersDelegate = DisplayAnswers;

            // try some async wait logic
            answerstask.GetAwaiter().OnCompleted(() => listAnswersDelegate(answerstask.Result));
            answerstask.Wait();
        }
        /// <summary>
        /// the logic has returned from the api call so display the answers.
        /// </summary>
        /// <param name="answerlist"></param>
        public void DisplayAnswers(StackExchange.StacMan.StacManResponse <Answer> answerlist)
        {
            DataTable tableForAnswersListview = new DataTable();

            tableForAnswersListview.Columns.Add("Answer", System.Type.GetType("System.String"));
            tableForAnswersListview.Columns.Add("AcceptedAnswer", System.Type.GetType("System.String"));
            tableForAnswersListview.Columns.Add("QuestionId", System.Type.GetType("System.String"));
            tableForAnswersListview.Columns.Add("AnswerId", System.Type.GetType("System.String"));

            foreach (Answer answer in answerlist.Data.Items)
            {
                tableForAnswersListview.Rows.Add(new object[] { answer.Body, answer.IsAccepted, answer.AnswerId });
            }

            StackOverFlowAnswersListView.DataSource = tableForAnswersListview;
            StackOverFlowAnswersListView.DataBind();
        }
        static void Main(string[] args)
        {
            StackExchange.StacMan.StacManClient stackAppClient = new StackExchange.StacMan.StacManClient("IWxKGhjr9yw)JT7GHEQGaA((", "2.1");

            DateTime startDate = DateTime.Now.AddDays(-1);
            DateTime endDate   = DateTime.Now;

            //DateTime startDate = DateTime.Parse("2017-01-01 00:00:00");
            //DateTime endDate = DateTime.Parse("2020-09-27 23:59:59");

            Task <StackExchange.StacMan.StacManResponse <Question> > mainQueryQuestiontask = stackAppClient.Search.GetMatchesAdvanced("stackoverflow.com", "withbody", null, null, startDate, endDate, null, null, null, null, null, null, null, true, 2);

            StacManResponse <Question> listOfQuestions = mainQueryQuestiontask.Result;

            List <int> listOfQuestionIds = new List <int>();

            foreach (Question q in listOfQuestions.Data.Items)
            {
                listOfQuestionIds.Add(q.QuestionId);
            }

            // now we have a list of questions with an accepted answer and more than one answer.
            // for each question
            // get the question title and question body  .. this will be one row.

            // get the question title and bodies and accepted answer id associated with list of question ids.
            Task <StackExchange.StacMan.StacManResponse <Question> > questionstask = stackAppClient.Questions.GetByIds("stackoverflow.com", listOfQuestionIds, "withbody");

            StackExchange.StacMan.StacManResponse <Question> listOfQuestionInfo = questionstask.Result;



            Task <StackExchange.StacMan.StacManResponse <Answer> > answerstask = stackAppClient.Questions.GetAnswers("stackoverflow.com", listOfQuestionIds, "withbody");

            StackExchange.StacMan.StacManResponse <Answer> listOfAnswerInfo = answerstask.Result;



            List <int> answers = new List <int>();
            List <int> questionsWithAcceptedanswer = new List <int>();

            Dictionary <int, List <int> > allAnswersWithQuestions          = new Dictionary <int, List <int> >();
            Dictionary <int, List <int> > allAnswersWithQuestionsFinalList = new Dictionary <int, List <int> >();

            //foreach (var answer in listOfAnswers.Data.Items)
            //{
            //    Console.WriteLine(" Quesiton Is " + answer.QuestionId.ToString() + " answer is " + answer.AnswerId.ToString());

            //    if (allAnswersWithQuestions.ContainsKey(answer.QuestionId) == false)
            //    {
            //        allAnswersWithQuestions.Add(answer.QuestionId, new List<int>());
            //    }

            //    allAnswersWithQuestions[answer.QuestionId].Add(answer.AnswerId);

            //    if (answer.IsAccepted == true)
            //    {
            //        if (questionsWithAcceptedanswer.Contains(answer.QuestionId) == false)
            //        {
            //            questionsWithAcceptedanswer.Add(answer.QuestionId);
            //            Console.WriteLine(" Quesiton With accepted answer : Question is " + answer.QuestionId.ToString() + " accepted answer is " + answer.AnswerId.ToString());
            //        }
            //    }

            //    //Console.WriteLine(answer.Title);
            //    //Console.WriteLine(answer.Comments);
            //    //Console.WriteLine(answer.QuestionId);
            //}

            //if (questionsWithAcceptedanswer.Count > 0)
            //{
            //Console.WriteLine("There are questions with an accepted answer");

            // loop throught questions with accepted answer;
            //  find that question in the first list and copy it to the final list;

            //foreach (int question in allAnswersWithQuestions.Keys)
            //{
            //    allAnswersWithQuestionsFinalList.Add(question, allAnswersWithQuestions[question]);
            //    Console.WriteLine("Question with accepted answer to final list is " + question.ToString());


            //    List<int> tempQuestion = new List<int>();
            //    tempQuestion.Add(question);

            //    Task<StackExchange.StacMan.StacManResponse<Question>> questiontask = stackAppClient.Questions.GetByIds("stackoverflow.com", tempQuestion, "withbody");
            //    StackExchange.StacMan.StacManResponse<Question> returnedQuestion = questiontask.Result;


            //    //int theanswers = returnedQuestion.Data.Items.Length;

            //    Question q2 = (Question)returnedQuestion.Data.Items[0];

            //    string questiontitle = q2.Title;

            //    //int? acceptedAnswerID = q2.AcceptedAnswerId;
            //    //int? numberAnswers = q2.AnswerCount;

            //    List<int> answerList = new List<int>();


            //    //for (int index = 1; index <= numberAnswers; index++)
            //    //{
            //      //  int? answer = q2.Answers[index - 1].AnswerId;
            //    answerList.Add(allAnswersWithQuestions[question][0]);
            //    //                  }

            //    // from this build the IEnumberable<int> and then then
            //    Task<StackExchange.StacMan.StacManResponse<Answer>> answertask = stackAppClient.Answers.GetByIds("stackoverflow.com", answerList, "withbody");

            //    StackExchange.StacMan.StacManResponse<Answer> returnedAnswer = answertask.Result;

            //    string answerBody = returnedAnswer.Data.Items[0].Body;


            //}

            //IEnumerable<int> queryQuestionsWithAcceptedAnswer =
            //from answer in allAnswersWithQuestions.Keys
            //from question in questionsWithAcceptedanswer where questionsWithAcceptedanswer.Contains(answer) select answer;

            //IEnumerator<int> iter = queryQuestionsWithAcceptedAnswer.GetEnumerator();

            //while (true)
            //{
            //  if (iter.MoveNext() == true)
            //Console.WriteLine("Question with accepted answer from main collection  is " + iter.Current);
            //}
            //}
            //var questions = stackAppClient.Questions.GetByIds("stackoverflow", answers).Result;
            //StringBuilder str = new StringBuilder();

            //foreach (var question in questions.Data.Items)
            //{
            //    str.Append(question.Title);
            //    str.Append("  ");
            //    str.AppendLine(question.Body);
            //    string line = str.ToString();

            //    Console.WriteLine(line);
            //}
        }