2

我想遍历 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);
    }
}    

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

4

4 回答 4

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

    }
}

这就是你所追求的吗?

于 2012-07-24T07:31:08.937 回答
1

既然你似乎想用 linq 来做,我们开始吧:

var tagged = Jobseekers.Select((x, i) => new { x, i });
var grouped = tagged.ToLookup(t => (t.i - 1) / 3, t => t.x);

foreach (var group in grouped)
{
    Console.WriteLine("Group:");
    foreach (var item in group)
    {
        Console.WriteLine(item);
    }
}
于 2012-07-24T07:31:05.883 回答
0

从以下答案:Split List into Sublists with LINQ

public static List<List<MemberProfile>> Split(List<MemberProfile> source)
{
    return  source
        .Select((x, i) => new { Index = i, Value = x })
        .GroupBy(x => x.Index / 3)
        .Select(x => x.Select(v => v.Value).ToList())
        .ToList();
}

也可以查看其他答案

于 2012-07-24T07:30:15.267 回答
0

您的问题并不完全清楚,但您似乎希望将jobseekers列表分成每组 3 个元素,然后对每组 3 个元素执行一些操作以获取一些 HTML 片段,因此输出是一系列 HTML 片段.

让我们假设它jobseekers总是有 3 个元素的倍数。如果它并不总是有 3 个元素,则此解决方案将保留最后一个组以及其余元素,您可以通过删除任何少于 3 个元素的组来过滤掉该组(即Where紧随GroupBy. 使用 LINQ:

IEnumerable<string> htmlSnippets = jobseekers
    .Zip(Enumerable.Range(0, jobseekers.Count), Tuple.Create)
    .GroupBy(tup => tup.Item2 / 3)
    .Select(GenerateHtmlString);
string combinedHtmlString = string.Join(string.Empty, htmlSnippets);

public string GenerateHtmlString(IEnumerable<MemberProfile> profiles)
{
    return string.Format(@"<div class=""jobseeker"">{0}</div>",
        // The question doesn't specify how the 3 jobseekers are rendered in HTML
        );
}
于 2012-07-24T07:32:11.543 回答