views:

62

answers:

4

Hi,

I have a problem here, Basically. .

  • I have a loop which has a list of sites, im going through each and every site.
  • I have another loop which only contains some sites
  • I want to return only the sites where the attribute t.title == F.title
  • If this is true I want to tick a check box
  • if not then dont tick a check box

The problem is, it keeps creating more checkboxes than I want, I only want the ones where there are matches ticked - the rest unticked?

        foreach (Admin.DTO.SiteMap t in sites)
        {

            for each (Admin.DTO.SmallSites f in smallsites){

            if (t.Title == f.Title)
            {
                myString1.Append(" <input type='checkbox'  checked='yes' value='"     +           t.Title + "'/> ");
                myString1.Append(t.Title);
            }
            else {

                myString1.Append(" <input type='checkbox'  value='" + t.Title +        "'/> ");
                myString1.Append(t.Title);
            }

              } 
        }
+1  A: 

If you don't want to display unchecked boxes then remove the else clause.

Steve Claridge
I want to display unchecked boxes though
Calibre2010
A: 

Calibre - your logic is flawed above.

basically you are always resetting each checkbox everytime you go round the second loop, thus something that was set in the previous loop is likely to get unset (and vice a versa). as noted, remove the else section (or preinitialise the values in a seperate statement)

[edit] - update with example:

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
    internal class Site
    {
        public string Title { get; set; }
        public string OtherStuff { get; set; }
        // don't know your site sturcture but this might help
    }
    class Program
    {
        static void Main(string[] args)
        {
            var siteMap =
                new List<Site>
                  {
                      new Site {OtherStuff = "bla1", Title = "Title 1"},
                      new Site {OtherStuff = "bla2", Title = "Title 2"},
                      new Site {OtherStuff = "bla3", Title = "Title 3"},
                      new Site {OtherStuff = "bla4", Title = "Title 4"},
                      new Site {OtherStuff = "bla5", Title = "Title 5"}
                  };
            var smallSites =
                new List<Site>
                  {
                      new Site {OtherStuff = "bla1", Title = "Title 1"},
                      new Site {OtherStuff = "bla2", Title = "Another title 2"},
                      new Site {OtherStuff = "bla3", Title = "Another title 3"},
                      new Site {OtherStuff = "bla4", Title = "Title 4"},
                      new Site {OtherStuff = "bla5", Title = "Another title 5"}
                  };

            // group all the matching titles to be checkboxed
            var query = (siteMap.Select(sm => 
                new {
                      sm.Title,
                      SmallSites = smallSites.Where(ss => ss.Title == sm.Title)
                  }));

            // get any items that don't match
            IEnumerable<Site> query2 =
                smallSites.Where(ss => !(siteMap.Select(sm => sm.Title))
                                           .Contains(ss.Title));

            string myString1 = "";
            // this is our checkbox items, do those 1st
            foreach (var item in query)
            {
                if (item.SmallSites.Any())
                {
                    foreach (var smallSite in item.SmallSites)
                    {
                        myString1 += string.Format("<input type='checkbox'  checked='yes' value='{0}'", smallSite.Title) + Environment.NewLine;
                    }
                }
            }

            // now throw down our non-checked items
            foreach (var smallSite in query2)
            {
                myString1 += string.Format("<input type='checkbox' value='{0}'", smallSite.Title) + Environment.NewLine;
            }
            Console.Write(myString1);
            Console.Read();
        }
    }
}

you could of course add each of the two items to a new list (i.e. rather than appending to myString1) as you go along and then sort the final list as you pleased and then spit that out as you wanted at the end.

cheers

jim
I want to display both checked and unchecked boxes but just depending on if the if statement is equal, how would i preinitialise the values?
Calibre2010
c- i've updateed my answer with a quick example of how you could attack this issue
jim
this looks good. Nice work, cheers
Calibre2010
+1  A: 

These two loop will create sites * smallsites checkboxs. With one few of it checked, many are not check in the rest.

Is this what you need?

foreach (Admin.DTO.SiteMap t in sites)
    {
        flg = false;
        for each (Admin.DTO.SmallSites f in smallsites)
            if (t.Title == f.Title) flg = true;
        if (flg)
        {
            myString1.Append(" <input type='checkbox'  checked='yes' value='"     +           t.Title + "'/> ");
            myString1.Append(t.Title);
        }
        else {

            myString1.Append(" <input type='checkbox'  value='" + t.Title +        "'/> ");
            myString1.Append(t.Title);
        }
    }
pinichi
cheers, I think thats the solution
Calibre2010
A: 

Your code is going to print out the contents of smallsites n times - where n is the number of items in sites. Each time it prints out smallsites, it might check one of the boxes.

I'm guessing you want to keep the outer loop as it, but then just check to see if the site is contained in smallsites. The reponse from pinichi will produce the correct output, but probably quite inefficiently. Without knowing the structure of SiteMap and SmallSites, I can't suggest a better method.

Paul Spangle