locked
if else inside foreach loop RRS feed

  • Question

  • User-323149085 posted

    hello ,

    im trying to run an if else condition inside a foreach loop(razor view) and the results are doubled (I understand more or less why) 

    im trying to find a way not to doubled the results.

    code and explain  ( I hope my question is clear ) 

     @foreach (var item in Qeustion2Buyer.Where(a => a.QuoteId == reqs.Id))
     						@* results://there are 3 Questions*@
                                            {
                                                <div class="list-group-item targetDiv" style="display: none">
    						 <p>@item.Question</p>
                                                    @foreach (var answers in AnswerToSeller)  @*results ://2 Questions has answer (onw answer for each one)*@
                                                    {
                                                        if (answers.QuoteQuestionId == item.QuoteQuestionId)
                                                        {
                                                            @answers.Answer
                                                        }
                                                        else
                                                        {
                                                            <button class="btnTestModel btn btn-primary btn-sm btnWhiteSpace " data-id="@item.QuoteQuestionId" style="width: 100%" type="button">
                                                                <i class="fas fa-comment" aria-hidden="true"></i>
                                                            </button>
                                                        }
                                                    }
                                                </div>
    @*results :// At  the view im getting regardless the answer results 2 
    or more buttons next to each qustion regardles if it was answerd*@

    thanks 

    Tuesday, December 11, 2018 2:51 PM

Answers

  • User-323149085 posted

    Thanks for your help. I just hack it differently 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2018 9:32 AM

All replies

  • User475983607 posted

    The AnswerToSeller loop runs for every item in...

    @foreach (var item in Qeustion2Buyer.Where(a => a.QuoteId == reqs.Id))

    I assume there should be a relationship between Qeustion2Buyer and AnswerToSeller but there is not enough information to provide a design suggestion.

    Tuesday, December 11, 2018 3:13 PM
  • User-323149085 posted

    Thanks for your replay

    yes your right , about the  why  ,  

    the classes look like this :

     public class QuoteQuestion
        {
            [Key]
            public int QuoteQuestionId { get; set; }
          
            public int QuoteId         { get; set; }
            public virtual QuettaReq QuettaReq { get; set; }
            public string SiteUserId { get; set; }
            public string Question { get; set; }
            public virtual IList<Answers> Answers { get; set; }
    
        }
    
     public class Answers
        {
            public int AnswersId { get; set; }
            public int QuoteQuestionId { get; set; }
            public string Answer { get; set; }
            public int QuoteId { get; set; }
    
        }

    Tuesday, December 11, 2018 3:32 PM
  • User1520731567 posted

    Hi john_mm,

    Actually, loop nested loops are not difficult to understand.

    After the sub-loop is executed, the parent loop enters the next round until the parent loop is executed.

    @foreach (var item in Qeustion2Buyer.Where(a => a.QuoteId == reqs.Id))
    @foreach (var answers in AnswerToSeller)

    Qeustion2Buyer,AnswerToSeller is unclear.

    I suggest you could add some breakpoints in loop and check if-else condition.

    Best Regards.

    Yuki Tao

    Wednesday, December 12, 2018 8:40 AM
  • User-323149085 posted

    Thanks for your help. I just hack it differently 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 13, 2018 9:32 AM