【问题标题】:alternative to c# foreach loop to get items from list替代 c# foreach 循环从列表中获取项目
【发布时间】:2019-02-25 12:02:27
【问题描述】:

我希望有人可以在这里帮助解决一个简单的问题。我已经寻找答案,但我认为部分问题是我不完全知道我在问什么。我有以下代码。

@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Xml.Linq;

<div>
@{ 
  String URLString = "https://api3.libcal.com/api_hours_grid.php?iid=4246&  format=xml&weeks=1&systemTime=0";
  XDocument xdoc = new XDocument();
  xdoc = XDocument.Load(URLString);


  var location = (from p in xdoc.Descendants("location")
                  from s in p.Descendants("week").Elements()
                  select new
                  {                  
                      CampusName = (string)p.Element("name"),
                      WeekD = (string)s.Element("date")
                  }).ToList();


                  foreach (var item in location)

                  {
                     foreach (var item in location)
                     {
                     <p>@item.WeekD</p>
                     }

                    <h2>@item.CampusName</h2>

                  }
}

</div>

显然嵌套的 foreach 循环会导致错误。我想要达到的目标如下

<h2>Campus Name</h2>
<p>date</p>
<p>date</p>

<h2>Campus Name</h2>
<p>date</p>
<p>date</p>

等等,有人可以就我如何获得这个结果提供一些建议。

【问题讨论】:

  • 为什么你定义了两个嵌套的foreach (var item in location)

标签: c# asp.net-mvc arraylist foreach


【解决方案1】:

你可以尝试GroupBy你的CampusName然后将你的结果投影到ToDictionarylike

@using System;
@using System.Collections.Generic;
@using System.Linq;
@using System.Xml.Linq;

<div>
    @{
        String URLString = "https://api3.libcal.com/api_hours_grid.php?iid=4246&  format=xml&weeks=1&systemTime=0";
        XDocument xdoc = new XDocument();
        xdoc = XDocument.Load(URLString);


        var location = (from p in xdoc.Descendants("location")
                        from s in p.Descendants("week").Elements()
                        select new
                        {
                            CampusName = (string)p.Element("name"),
                            WeekD = (string)s.Element("date")
                        }).ToList();


        var result = location.GroupBy(x => x.CampusName)
               .ToDictionary(grp => grp.Key, grp => grp.Select(x => x.WeekD));


        foreach (var item in result)
        {
            <h2>@item.Key</h2>
            foreach (var innerItem in item.Value)
            {
                <p>@innerItem</p>
            }
        }
    }

</div>

输出:输出包含 6 行位置名称,但为了简单起见,这里我只显示 2 行

【讨论】:

  • @IcantCode,查看答案可能对您有帮助:)
【解决方案2】:

(伪代码)

string currentLocation = "";

foreach (var item in location)
{
   if(@item.CampusName != currentLocation)
   {
      <h2>@item.CampusName</h2>
      currentLocation = @item.CampusName;
   }
   <p>@item.WeekD</p>
}

【讨论】:

  • @item.CampusName = currentLocation;改成currentLocation = @item.CampusName;(你实际上想更新currentLocation而不是item.CampusName
  • 太棒了!非常感谢 PhillipH 和 Rafalon 这得到了它,但我不得不使用

    @item.CampusName

    currentLocation = @item.CampusName;
【解决方案3】:

我为你制作了代码。试试吧!

    string URLString = "https://api3.libcal.com/api_hours_grid.php?iid=4246&%20format=xml&weeks=1&systemTime=0";
    XDocument xdoc = XDocument.Load(URLString );

    foreach (var element in xdoc.Descendants("location"))
    {
        var temp = element.Descendants("name");
        string name = "";
        if (temp.Count() != 0)
            name = temp.ToList()[0].Value;
        <h2>@name</h2>
        foreach (var d in element.Descendants("date"))
        {
            var date = d.Value;
            <p>date</p>
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-03
    • 2015-12-06
    • 2021-07-12
    • 2010-11-30
    • 2011-06-23
    • 2020-06-08
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多