这里有一个棘手的问题 - 尝试遍历一个循环并且仅返回值等于另一个循环的值

发布于 2024-09-25 19:30:49 字数 822 浏览 0 评论 0原文

我这里有一个问题,基本上。 。

  • 我有一个循环,其中有一个站点列表,我正在浏览每个站点。
  • 我有另一个循环,仅包含一些网站
  • 我只想返回属性 t.title == F.title 的网站
  • 如果这是真的,我想勾选一个复选框,
  • 如果不是,则不要勾选复选框

问题是,它不断创建比我想要的更多的复选框,我只想要勾选匹配项的复选框 - 其余的未勾选?

        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);
            }

              } 
        }

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);
            }

              } 
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在风中等你 2024-10-02 19:30:49

这两个循环将创建站点 * 小站点复选框。检查了其中的一些,其余的许多都没有检查。

这是您需要的吗?

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);
        }
    }

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);
        }
    }
野の 2024-10-02 19:30:49

如果您不想显示未选中的框,请删除 else 子句。

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

唯憾梦倾城 2024-10-02 19:30:49

您的代码将打印 smallsites 的内容 n 次 - 其中 n 是 sites 中的项目数。每次打印出smallsites时,它可能会选中其中一个框。

我猜您想保持外部循环不变,但随后只需检查该站点是否包含在 smallsites 中。 pinichi 的响应将产生正确的输出,但可能效率很低。在不了解 SiteMapSmallSites 结构的情况下,我无法提出更好的方法。

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.

悲凉≈ 2024-10-02 19:30:49

口径 - 你的逻辑在上面有缺陷。

基本上,每次进行第二个循环时,您总是会重置每个复选框,因此在前一个循环中设置的内容可能会被取消设置(反之亦然)。如前所述,删除 else 部分(或在单独的语句中预初始化值)

[编辑] - 用示例更新:

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();
        }
    }
}

您当然可以将这两个项目中的每一个添加到新列表中(即而不是附加到 myString1) 中,然后根据需要对最终列表进行排序,然后在最后根据需要将其吐出。

干杯

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文