为了账号安全,请及时绑定邮箱和手机立即绑定

C# Linq,包括排序和分组

C# Linq,包括排序和分组

C#
UYOU 2021-11-07 19:30:56
这是我的代码: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 行。)
查看完整描述

3 回答

?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

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


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);

}


查看完整回答
反对 回复 2021-11-07
?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

请稍等!这不是最终答案!需要进一步修改!正在进行修改。


List<Photos> photolist = new List<Photos>()

        {

            new Photos() {UserID = 92, PhotoLabel = 20180729181046},

            new Photos() {UserID = 92, PhotoLabel = 20180729181041},

            new Photos() {UserID = 92, PhotoLabel = 20180729181037},

            new Photos() {UserID = 88, PhotoLabel = 20180729174415},

            new Photos() {UserID = 88, PhotoLabel = 20180729174405},

            new Photos() {UserID = 04, PhotoLabel = 20180729174358},

            new Photos() {UserID = 1, PhotoLabel = 20170924183847},

            new Photos() {UserID = 1, PhotoLabel = 20170921231422},

            new Photos() {UserID = 1, PhotoLabel = 20170920194624},

            new Photos() {UserID = 32, PhotoLabel = 20170820114728},

            new Photos() {UserID = 32, PhotoLabel = 20170820114725},

            new Photos() {UserID = 32, PhotoLabel = 20170820114421},

            new Photos() {UserID = 32, PhotoLabel = 20170820114416},

            new Photos() {UserID = 1, PhotoLabel = 20170225151023},

            new Photos() {UserID = 1, PhotoLabel = 20170225151000},

        };    


var photolist2 = photolist.GroupBy(g => g.UserID)

                .Select(p => new

                {

                    UserId = p.Key,

                    Count = p.Count()


                })

                .ToList();



var filteredPhotoList = photolist 

            .Join(photolist2, 

                photo => photo.UserID, 

                photo2 => photo2.UserId, 

                (photo, photo2) => new {UserId = photo.UserID, PhotoLabel = 

                photo.PhotoLabel, Count = photo2.Count})

            .Where(p => p.Count > 2).Select(p => new

            {

                p.UserId, p.PhotoLabel

            }).OrderByDescending(p => p.PhotoLabel).ThenBy(p => p.UserId).ToList();


查看完整回答
反对 回复 2021-11-07
?
喵喔喔

TA贡献1735条经验 获得超5个赞

SKLTFZ 和 TanvirArjel 的答案很接近,但没有达到预期的结果。我意识到你无法在 Linq 中实现上述所有内容,所以这就是我想出的,它实现了上面列出的所有内容:


PS:我将var result1重命名为ordered_photolist


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


        List<Photos> temp_photolist = new List<Photos>();

        List<Photos> final_photolist = new List<Photos>();

        int UserID = -1;

        int UserIDCount = 0;


        foreach (Photos p in ordered_photolist)

        {

            if (UserID == -1)

            {

                UserID = p.UserID;

                temp_photolist.Add(p);

                UserIDCount++;

            }

            else

            {

                if ( UserID == p.UserID )

                {

                    temp_photolist.Add(p);

                    UserIDCount++;

                }

                else

                {

                    if ( UserIDCount >= 3 )

                    {

                        // add temp_photolist to final list

                        int index = final_photolist.FindIndex(item => item.UserID == UserID);

                        if (index == -1)

                        {

                            // element does not exists, do what you need

                            final_photolist.AddRange(temp_photolist);

                        }


                        temp_photolist.Clear();

                        temp_photolist.Add(p);

                        UserIDCount = 1;

                        UserID = p.UserID;

                    }

                    else

                    {

                        temp_photolist.Clear();

                        UserIDCount = 0;

                        UserID = -1;

                    }

                }

            }



        }


查看完整回答
反对 回复 2021-11-07
  • 3 回答
  • 0 关注
  • 266 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信