locked
Foreach statements returning incorrect results - Linq to SQL RRS feed

  • Question

  • User940894612 posted

    Hi All

    I'm having a slight issue.  Please see code below.  I should only be returning 4 records from my feedback table and adding the comments to the relevant panel depending on whether it is divisible by 2.  

    However, I am getting 16 records appearing in the front end and I'm not sure why. a quick debug seems to show that it executes the select statement which returns the 4 records and then does my foreach statement on all of them.  

    I essentially want it to get one record and then display the relevant comments, then get the second record and do the same and third, fourth etc.  Any ideas on where I have gone wrong? 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Testimonials1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AltanaDataClassesDataContext db = new AltanaDataClassesDataContext();
    
            var testimonials = from t in db.Feedbacks
                where t.OnSite == true
                select t;
    
            int count = testimonials.Count();
            foreach (var tesimonial in testimonials)
            {
    
                for (int i = 0; i < count; i++)
                {
                    int y = i % 2;
    
                    if (y == 0)
                    {
                        Literal literal = new Literal();
                        literal.Text = "<div class='row'><div class='col-lg-6'>0" + Server.HtmlDecode(tesimonial.Comments) + "</div>";
                        pnlTestimonials.Controls.Add(literal);
                    }
                    else
                    {
                        //Response.Write(y + " b ");
                        Literal literal = new Literal();
                        literal.Text = "<div class='col-lg-6'>1 " + Server.HtmlDecode(tesimonial.Comments) + "</div></div>";
                        pnlTestimonials.Controls.Add(literal);
                    }
                }
            }
        }
    }

    Thanks

    Rob

    Thursday, November 1, 2018 9:11 PM

All replies

  • User475983607 posted

    That are two nested loops code so you end up with X^2 iterations.  Remove one of the loops.

            int count = testimonials.Count();  
            foreach (var tesimonial in testimonials)  //LOOP 1
            {
    
                for (int i = 0; i < count; i++) // LOOP 2
                {

    Thursday, November 1, 2018 9:19 PM
  • User940894612 posted
    Hi

    Thanks for your response, can you show me how please?

    Thanks
    Rob
    Thursday, November 1, 2018 9:41 PM
  • User940894612 posted

    For example below:

    for (int i = 0; i < testimonials.Count(); i++) 
    { 
    int y = i % 2; if (y == 0) 
    { 
    Literal literal = new Literal(); literal.Text = "
    
    0" + Server.HtmlDecode(tesimonial.Comments) + "
    "; pnlTestimonials.Controls.Add(literal); } else { Literal literal = new Literal(); literal.Text = "
    1 " + Server.HtmlDecode(tesimonial.Comments) + "
    "; pnlTestimonials.Controls.Add(literal); 
    } 
    }

    How can I get the testimonial.Comment value as its not avaiable to me anymore?

    Thanks

    Rob

    Thursday, November 1, 2018 10:09 PM
  • User1520731567 posted

    Hi Robbied81,

    Robbied81

    foreach (var tesimonial in testimonials)
            {
    
                for (int i = 0; i < count; i++)
                {
                    int y = i % 2;

    According to your code:

    Nested loop in loop,Record will increase exponentially

    If you would like to filter even and odd lines,you could:

    int i = 0;
    foreach (var tesimonial in testimonials)
    {
      if(i%2==0)
    {}
    else
    {}
    ++i; }

    Best Regards.

    Yuki Tao

    Friday, November 2, 2018 9:49 AM