gpt4 book ai didi

c# - 如何遍历通用 List 并按 3 项进行分组

转载 作者:行者123 更新时间:2023-11-30 18:55:05 24 4
gpt4 key购买 nike

我想遍历 MemberProfiles 的通用列表:

List<MemberProfile> jobseekers    

,并将项目分组在 <div class='group'> 中标签。每个<div class="group">应该包含 3 个这样的求职者:

<div class="group">
<div class="jobseeker">
...data jobseeker
</div>
</div>

我在通用列表上尝试了不同的方法,例如 .skip() 和 .take() 但我没有成功实现这种情况。

我的代码是这样的:

foreach (MemberProfile jobseekerProfile in Jobseekers)
{
if (jobseekerProfile != null)
{
Label lblJobseeker = new Label();
StringBuilder sbJobseeker = new StringBuilder();
sbJobseeker.Append(string.Format("<p><strong>{0}&nbsp;{1}</strong><br />", jobseekerProfile.FirstName, jobseekerProfile.LastName));
XPathNodeIterator preValues = library.GetPreValues(1362);
preValues.MoveNext();
XPathNodeIterator iterator2 = preValues.Current.SelectChildren("preValue", "");
while (iterator2.MoveNext())
{
if (jobseekerProfile.JobType == iterator2.Current.GetAttribute("id", ""))
{
sbJobseeker.Append(string.Format("looking for a {0}<br />", iterator2.Current.Value));
}
}
XPathNodeIterator iterator3 = library.GetPreValues(1363);
iterator3.MoveNext();
XPathNodeIterator iterator4 = iterator3.Current.SelectChildren("preValue", "");
StringBuilder sbJobExperience = new StringBuilder();
string[] strJobExperience = jobseekerProfile.JobExperience.Split(new char[] { ',' });
int counter = 1;
while (iterator4.MoveNext())
{
if (jobseekerProfile.JobExperience.Contains(iterator4.Current.GetAttribute("id", "")))
{
if (counter != strJobExperience.Count<string>())
{
sbJobExperience.Append(string.Format("{0}, ", iterator4.Current.Value));
counter++;
}
else
{
sbJobExperience.Append(string.Format("{0}", iterator4.Current.Value));
}
}
}
sbJobseeker.Append(string.Format("Fields of experience: {0}<br />", sbJobExperience.ToString()));
sbJobseeker.Append(string.Format("Years of experience: {0}<br />", jobseekerProfile.YearsExperience));
sbJobseeker.Append(string.Format("Country: {0}<br />", jobseekerProfile.Country));
sbJobseeker.Append(string.Format("<form name='frmSelect' action='/selectjobcandidate.aspx' method='post'><input type='hidden' name='username' value='{0}' /><input type='submit' value='select candidate' /></form>", jobseekerProfile.UserName));
lblJobseeker.Text = sbJobseeker.ToString();
phListJobseekers.Controls.Add(lblJobseeker);
}
}

有人可以让我走上正确的轨道来实现这个场景吗?

最佳答案

for (var i = 0; i < Jobseekers.Count; i += 3)
{
foreach (MemberProfile jobseekerProfile in Jobseekers.Skip(i).Take(3))
{

}
}

这就是你想要的吗?

关于c# - 如何遍历通用 List<T> 并按 3 项进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11625938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com