如何对我的列表进行分组<>分类

时间:2012-02-16 12:41:47

标签: c# asp.net

我有以下课程:

public class TV
{
    public string GroupId { get; set; }
    public string Title { get; set; }
    public string Genre { get; set; }
}

然后我有以下列表(动态创建并且永远不会相同):

List<TV> myList = new List<TV>();
myList.Add(new TV { GroupId = "1", Title = "Title 1", Genre = "Genre A" });
myList.Add(new TV { GroupId = "1", Title = "Title 2", Genre = "Genre A" });
myList.Add(new TV { GroupId = "1", Title = "Title 3", Genre = "Genre A" });
myList.Add(new TV { GroupId = "3", Title = "Title 4", Genre = "Genre 18" });
myList.Add(new TV { GroupId = "A", Title = "Title 5", Genre = "Genre 18" });
myList.Add(new TV { GroupId = "A", Title = "Title 6", Genre = "Genre A" });
myList.Add(new TV { GroupId = "B", Title = "Title 7", Genre = "Genre A" });
myList.Add(new TV { GroupId = "C", Title = "Title 8", Genre = "Genre A" });
myList.Add(new TV { GroupId = "D", Title = "Title 9", Genre = "Genre 18" });
myList.Add(new TV { GroupId = "D", Title = "Title 10", Genre = "Genre A" });
myList.Add(new TV { GroupId = "D", Title = "Title 11", Genre = "Genre A" });
myList.Add(new TV { GroupId = "E", Title = "Title 12", Genre = "Genre A" });

我有以下(硬编码)导航类别是链接:

  

[0-9] [ABC] [DEF] [GHI] [JKL] [MNO] [PQR] [STU] [VWXYZ]

我正在尝试循环myList并查看 GroupId 属性,我想将它放在相应的div中,所以我最终得到这样的div:

<div id="group-09">
<div>
    1, Title 1, Genre A
    1, Title 2, Genre A
    1, Title 3, Genre A
    3, Title 4, Genre 18
</div>
</div>

<div id="group-ABC">
<div>
    A, Title 5, Genre 18
    A, Title 6, Genre A
    B, Title 7, Genre A
    C, Title 8, Genre A
</div>
</div>

<div id="group-DEF">
<div>
    D, Title 9, Genre 18
    D, Title 10, Genre A
    D, Title 11, Genre A
    E, Title 12, Genre A
</div>
</div>

我似乎无法弄清楚正确执行此操作的逻辑! 有没有人有建议? 感谢

4 个答案:

答案 0 :(得分:2)

public string GetGroupFamilyId(string groupId) {
   if (groupId >= "A" && groupId <="C") return "ABC";
   .....
   //or implement it somehow, implementation does not matter, the idea does
}

....

var groupedResult = myList
                      .GroupBy(x=>GetGroupFamilyId(x));

看起来这就是你所需要的......

答案 1 :(得分:1)

您可以创建一个字典,将GroupId映射到<div> ID,并将myList中的项目分组为<div> id:

var groups = new Dictionary<string, string>
{
    { "0", "group-09" },
    { "1", "group-09" },
    { "2", "group-09" },
    ...
    { "A", "group-ABC" },
    ...
};

var query = from item in myList
            group item by groups[item.GroupId];

foreach (var group in query)
{
    Console.WriteLine("id: " + group.Key);
    foreach (var item in group)
    {
        Console.WriteLine("       " + item.Title);
    }
}

答案 2 :(得分:1)

使用Regex

        string key = "[0-9] [ABC] [DEF] [GHI] [JKL] [MNO] [PQR] [STU] [VWXYZ]";
        string[] keyes = key.Split(' ');

        Dictionary<string, IEnumerable<TV>> groups = new Dictionary<string, IEnumerable<TV>>();

        foreach (var pattern in keyes)
        {
            Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);

            groups.Add(pattern, myList.Where(x => reg.IsMatch(x.GroupId)));
        }

这也是DIV的解析:

        StringBuilder sb = new StringBuilder();
        foreach (var group in groups.Where(x => x.Value.Any()))
        {
            sb.AppendFormat(CultureInfo.CurrentUICulture, "<div id=\"group-{0}\">\n\t<div>", group.Key.Split(new char[] { '[', ']'})[1]);

            foreach (var tv in group.Value)
            {
                sb.AppendFormat(CultureInfo.CurrentUICulture, "\n\t\t{0}", tv.ToString());
            }
            sb.AppendFormat("\n\t</div>\n</div>\n\n");
        }

示例输出:

<div id="group-0-9">
    <div>       
        1, Title 1, Genre A     
        1, Title 2, Genre A     
        1, Title 3, Genre A     
        3, Title 4, Genre 18
    </div>
</div>
<div id="group-ABC">
    <div>       
        A, Title 5, Genre 18        
        A, Title 6, Genre A     
        B, Title 7, Genre A     
        C, Title 8, Genre A
    </div>
</div>
<div id="group-DEF">
    <div>       
        D, Title 9, Genre 18        
        D, Title 10, Genre A        
        D, Title 11, Genre A        
        E, Title 12, Genre A
    </div>
</div>

答案 3 :(得分:0)

攻击的第一个角度是将所有内容都集成到一个分组对象中:

IEnumerable<IGrouping<string, TV>> newGroup = myList.GroupBy(x => x.GroupId);

然后,您可以根据您的仲裁组[abc]等将列表连在一起:

var listABC = newGroup.Where(y => 
                y.Key.Contains("A") 
                || y.Key.Contains("B") 
                || y.Key.Contains("C"));

存在更好的方法,但这是一个值得思考的问题......