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     |


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


显示组 ofUserIDs和PhotoLabelswhereUserIDs在一个组中出现 3 次或更多次(例如:第 4 行和第 5UserID=88行where和第 6 行 whereUserID=04应该消除,因为它们UserID=88在组UserID=04中只出现两次,而在组中只出现一次)。


仅显示最上面的组UserIDs并排除任何重复UserIDs(例如:第 7、8 和 9 行显示该UserID=1组。不要显示任何其他UserID=1组,例如第 14、15 和 16 行。)


UYOU
浏览 267回答 3
3回答

哈士奇WWW

如果我没有误解要求,下面的功能可以正常工作(但它不应该是最有效的解决方案)protected List<AnObject> aFunction(List<AnObject> sortedList){&nbsp; &nbsp; //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).&nbsp; &nbsp; //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.&nbsp; &nbsp; int pivot = -1;&nbsp; &nbsp; int cnt = 0;&nbsp; &nbsp; List<AnObject> masterList = new List<AnObject>();&nbsp; &nbsp; List<AnObject> subList = new List<AnObject>();&nbsp; &nbsp; //List<int> Excluded = new List<int>();&nbsp; &nbsp; foreach (AnObject r in sortedList)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (pivot != r.UserID)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cnt > 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; masterList.AddRange(subList);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Excluded.Add(pivot);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subList.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pivot = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cnt = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if (!Excluded.Contains(r.UserID))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!masterList.Any(x => x.UserID == r.UserID))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pivot = r.UserID;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; subList.Add(r);&nbsp; &nbsp; &nbsp; &nbsp; cnt++;&nbsp; &nbsp; }&nbsp; &nbsp; return masterList;}调用它进行测试protected class AnObject{&nbsp; &nbsp; public AnObject(int uid, string photolabel)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.UserID = uid;&nbsp; &nbsp; &nbsp; &nbsp; this.PhotoLabel = photolabel;&nbsp; &nbsp; }&nbsp; &nbsp; public int UserID { get; set; }&nbsp; &nbsp; public string PhotoLabel { get; set; }}protected void Execute(){&nbsp; &nbsp; List<AnObject> sortedList = new List<AnObject>();&nbsp; &nbsp; sortedList.Add(new AnObject(92, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(92, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(92, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(88, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(88, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(4, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(1, "anystringfirst"));&nbsp; &nbsp; sortedList.Add(new AnObject(1, "anystringfirst"));&nbsp; &nbsp; sortedList.Add(new AnObject(1, "anystringfirst"));&nbsp; &nbsp; sortedList.Add(new AnObject(32, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(32, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(32, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(32, "anystring"));&nbsp; &nbsp; sortedList.Add(new AnObject(1, "anystringafter"));&nbsp; &nbsp; sortedList.Add(new AnObject(1, "anystringafter"));&nbsp; &nbsp; sortedList.Add(new AnObject(1, "anystringafter"));&nbsp; &nbsp; List<AnObject> bb = aFunction(sortedList);}

扬帆大鱼

请稍等!这不是最终答案!需要进一步修改!正在进行修改。List<Photos> photolist = new List<Photos>()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 92, PhotoLabel = 20180729181046},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 92, PhotoLabel = 20180729181041},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 92, PhotoLabel = 20180729181037},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 88, PhotoLabel = 20180729174415},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 88, PhotoLabel = 20180729174405},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 04, PhotoLabel = 20180729174358},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 1, PhotoLabel = 20170924183847},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 1, PhotoLabel = 20170921231422},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 1, PhotoLabel = 20170920194624},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 32, PhotoLabel = 20170820114728},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 32, PhotoLabel = 20170820114725},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 32, PhotoLabel = 20170820114421},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 32, PhotoLabel = 20170820114416},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 1, PhotoLabel = 20170225151023},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new Photos() {UserID = 1, PhotoLabel = 20170225151000},&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp;&nbsp;var photolist2 = photolist.GroupBy(g => g.UserID)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(p => new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserId = p.Key,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Count = p.Count()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList();var filteredPhotoList = photolist&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Join(photolist2,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo => photo.UserID,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo2 => photo2.UserId,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (photo, photo2) => new {UserId = photo.UserID, PhotoLabel =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; photo.PhotoLabel, Count = photo2.Count})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Where(p => p.Count > 2).Select(p => new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p.UserId, p.PhotoLabel&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).OrderByDescending(p => p.PhotoLabel).ThenBy(p => p.UserId).ToList();

喵喔喔

SKLTFZ 和 TanvirArjel 的答案很接近,但没有达到预期的结果。我意识到你无法在 Linq 中实现上述所有内容,所以这就是我想出的,它实现了上面列出的所有内容:PS:我将var result1重命名为ordered_photolist&nbsp; &nbsp; &nbsp; &nbsp; List<Photos> ordered_photolist = photolist.OrderByDescending(p => p.PhotoLabel).ThenBy(r => r.UserID).ToList();&nbsp; &nbsp; &nbsp; &nbsp; List<Photos> temp_photolist = new List<Photos>();&nbsp; &nbsp; &nbsp; &nbsp; List<Photos> final_photolist = new List<Photos>();&nbsp; &nbsp; &nbsp; &nbsp; int UserID = -1;&nbsp; &nbsp; &nbsp; &nbsp; int UserIDCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach (Photos p in ordered_photolist)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (UserID == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserID = p.UserID;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp_photolist.Add(p);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserIDCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( UserID == p.UserID )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp_photolist.Add(p);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserIDCount++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( UserIDCount >= 3 )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add temp_photolist to final list&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int index = final_photolist.FindIndex(item => item.UserID == UserID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (index == -1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // element does not exists, do what you need&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final_photolist.AddRange(temp_photolist);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp_photolist.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp_photolist.Add(p);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserIDCount = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserID = p.UserID;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp_photolist.Clear();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserIDCount = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; UserID = -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP