首页 > 解决方案 > 包括排序和分组的 C# Linq

问题描述

这是我的代码:

public class Photos
{
    public long PhotoLabel { get; set; }        
    public int UserID { get; set; }
}

List<Photos> photolist = new List<Photos>();
var result1 = photolist.OrderByDescending(p => p.PhotoLabel).ThenBy(r => r.UserID).ToList();

如果我现在显示内容,这就是我得到的(首先按降序排序,PhotoLabel然后按以下排序UserID

|------|---------------------|---------------------|
| Row  |     UserID          |    PhotoLabel       |
|----------------------------|---------------------|
| 1    |      92             |  20180729181046     |
|----------------------------|---------------------|
| 2    |      92             |  20180729181041     |
|----------------------------|---------------------|
| 3    |      92             |  20180729181037     |
|----------------------------|---------------------|
| 4    |      88             |  20180729174415     |
|----------------------------|---------------------|
| 5    |      88             |  20180729174405     |
|----------------------------|---------------------|
| 6    |      04             |  20180729174358     |
|----------------------------|---------------------|
| 7    |      1              |  20170924183847     |
|----------------------------|---------------------|
| 8    |      1              |  20170921231422     |
|----------------------------|---------------------|
| 9    |      1              |  20170920194624     |
|----------------------------|---------------------|
| 10   |      32             |  20170820114728     |
|----------------------------|---------------------|
| 11   |      32             |  20170820114725     |
|----------------------------|---------------------|
| 12   |      32             |  20170820114421     |
|----------------------------|---------------------|
| 13   |      32             |  20170820114416     |
|----------------------------|---------------------|
| 14   |      1              |  20170225151023     |
|----------------------------|---------------------|
| 15   |      1              |  20170225151000     |
|----------------------------|---------------------|
| 16   |      1              |  20170225150957     |
|----------------------------|---------------------|

从上面的排序表中,这就是我想要实现的目标:

查询的预期结果应该是:

|------|---------------------|---------------------|
| Row  |     UserID          |    PhotoLabel       |
|----------------------------|---------------------|
| 1    |      92             |  20180729181046     |
|----------------------------|---------------------|
| 2    |      92             |  20180729181041     |
|----------------------------|---------------------|
| 3    |      92             |  20180729181037     |
|----------------------------|---------------------|
| 7    |      1              |  20170924183847     |
|----------------------------|---------------------|
| 8    |      1              |  20170921231422     |
|----------------------------|---------------------|
| 9    |      1              |  20170920194624     |
|----------------------------|---------------------|
| 10   |      32             |  20170820114728     |
|----------------------------|---------------------|
| 11   |      32             |  20170820114725     |
|----------------------------|---------------------|
| 12   |      32             |  20170820114421     |
|----------------------------|---------------------|
| 13   |      32             |  20170820114416     |
|----------------------------|---------------------|

提前非常感谢您!:-)

标签: c#linq

解决方案


如果我没有误解要求,以下功能可以正常工作(但它不应该是最有效的解决方案)

protected List<AnObject> aFunction(List<AnObject> sortedList)
{
    //Display groups of UserIDs and PhotoLabels where UserIDs appear 3 or more times in one group (eg: rows 4 and 5 where UserID = 88 and row 6 where UserID = 04 should be eliminated since the UserID = 88 appears just twice in the group and UserID = 04 appears only once in the group).
    //Display only the top most group of UserIDs and exclude any repeating UserIDs(eg: rows 7, 8 and 9 displays the UserID = 1 group.Don't display any other UserID=1 group such as rows 14,15 and 16.
    int pivot = -1;
    int cnt = 0;
    List<AnObject> masterList = new List<AnObject>();
    List<AnObject> subList = new List<AnObject>();
    //List<int> Excluded = new List<int>();
    foreach (AnObject r in sortedList)
    {
        if (pivot != r.UserID)
        {
            if (cnt > 2)
            {
                masterList.AddRange(subList);
                //Excluded.Add(pivot);
            }
            subList.Clear();
            pivot = -1;
            cnt = 0;
            //if (!Excluded.Contains(r.UserID))
            if (!masterList.Any(x => x.UserID == r.UserID))
            {
                pivot = r.UserID;
            }
        }
        subList.Add(r);
        cnt++;
    }
    return masterList;
}

调用它进行测试

protected class AnObject
{
    public AnObject(int uid, string photolabel)
    {
        this.UserID = uid;
        this.PhotoLabel = photolabel;
    }
    public int UserID { get; set; }
    public string PhotoLabel { get; set; }
}
protected void Execute()
{
    List<AnObject> sortedList = new List<AnObject>();
    sortedList.Add(new AnObject(92, "anystring"));
    sortedList.Add(new AnObject(92, "anystring"));
    sortedList.Add(new AnObject(92, "anystring"));
    sortedList.Add(new AnObject(88, "anystring"));
    sortedList.Add(new AnObject(88, "anystring"));
    sortedList.Add(new AnObject(4, "anystring"));
    sortedList.Add(new AnObject(1, "anystringfirst"));
    sortedList.Add(new AnObject(1, "anystringfirst"));
    sortedList.Add(new AnObject(1, "anystringfirst"));
    sortedList.Add(new AnObject(32, "anystring"));
    sortedList.Add(new AnObject(32, "anystring"));
    sortedList.Add(new AnObject(32, "anystring"));
    sortedList.Add(new AnObject(32, "anystring"));
    sortedList.Add(new AnObject(1, "anystringafter"));
    sortedList.Add(new AnObject(1, "anystringafter"));
    sortedList.Add(new AnObject(1, "anystringafter"));
    List<AnObject> bb = aFunction(sortedList);
}

推荐阅读