基于键 C# 合并两个列表

发布于 2024-12-13 12:19:35 字数 1136 浏览 1 评论 0原文

我有班级奖励,我可以保留每场比赛的用户资金。 我有两个列表,我需要以某种方式连接起来。我在下面写了示例列表和期望的结果。

public class Reward
{
    public int Game { get; set; }
    public int User { get; set; }
    public int Money { get; set; }

    public Reward Merge(Reward p)
    {
       return new Reward { Game = this.Game, User = this.User, Money = this.Money + p.Money};
    }
}

    IList<Reward> list1 = new List<Reward>();
    list1.Add(new Reward {Game = 1, User = 1, Money = 10});
    list1.Add(new Reward { Game = 1, User = 2, Money = 20 });
    list1.Add(new Reward { Game = 1, User = 3, Money = 30 });


    IList<Reward> list2 = new List<Reward>();
    list2.Add(new Reward { Game = 2, User = 1, Money = 15 });
    list2.Add(new Reward { Game = 2, User = 2, Money = 25 });
    list2.Add(new Reward { Game = 2, User = 4, Money = 35 });

结果列表应该是

User Money
1    25
2    45
3    30
4    35

我尝试的

IList<Reward> listConcat = list1.Concat(list2)
                .GroupBy(u=> u.User)
                .Select(???)
                .ToList();

但如何?

I have class Reward which i keep user money per game.
I have two list somehow I need to concate. I write sample lists and desired result below.

public class Reward
{
    public int Game { get; set; }
    public int User { get; set; }
    public int Money { get; set; }

    public Reward Merge(Reward p)
    {
       return new Reward { Game = this.Game, User = this.User, Money = this.Money + p.Money};
    }
}

    IList<Reward> list1 = new List<Reward>();
    list1.Add(new Reward {Game = 1, User = 1, Money = 10});
    list1.Add(new Reward { Game = 1, User = 2, Money = 20 });
    list1.Add(new Reward { Game = 1, User = 3, Money = 30 });


    IList<Reward> list2 = new List<Reward>();
    list2.Add(new Reward { Game = 2, User = 1, Money = 15 });
    list2.Add(new Reward { Game = 2, User = 2, Money = 25 });
    list2.Add(new Reward { Game = 2, User = 4, Money = 35 });

Result List should be

User Money
1    25
2    45
3    30
4    35

I try

IList<Reward> listConcat = list1.Concat(list2)
                .GroupBy(u=> u.User)
                .Select(???)
                .ToList();

but how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

我不吻晚风 2024-12-20 12:19:35

您使用 GroupBy 的方式是正确的,这应该可以完成工作:

IEnumerable<Reward> result =
    from r in list1.Concat(list2)
    group r by r.User into groupedRewards
    select new Reward
    {
        Game = 0,   //what game to use?
        User = groupedRewards.Key,
        Money = groupedRewards.Sum(r => r.Money)
    };

编辑:与 lambda 表达式相同:

IEnumerable<Reward> result = list1.Concat(list2)
    .GroupBy(u => u.User)
    .Select(g => new Reward
    {
        Game = 0,  //what game to use?
        User = g.Key,
        Money = g.Sum(r => r.Money)
    });

You were on the right track with GroupBy, this should do the job:

IEnumerable<Reward> result =
    from r in list1.Concat(list2)
    group r by r.User into groupedRewards
    select new Reward
    {
        Game = 0,   //what game to use?
        User = groupedRewards.Key,
        Money = groupedRewards.Sum(r => r.Money)
    };

EDIT: Same thing with lambda expression:

IEnumerable<Reward> result = list1.Concat(list2)
    .GroupBy(u => u.User)
    .Select(g => new Reward
    {
        Game = 0,  //what game to use?
        User = g.Key,
        Money = g.Sum(r => r.Money)
    });
冧九 2024-12-20 12:19:35

这不是很漂亮,但是很有效。

var listConcat = list1.Concat(list2)
                .GroupBy(u=> u.User)
                .Select(rewards => new Reward { 
                                                User = rewards.Key, 
                                                Money = rewards.Select(reward => reward.Money).Sum() 
                                              }
                       )
                .ToList();

This isn't very pretty, but it works.

var listConcat = list1.Concat(list2)
                .GroupBy(u=> u.User)
                .Select(rewards => new Reward { 
                                                User = rewards.Key, 
                                                Money = rewards.Select(reward => reward.Money).Sum() 
                                              }
                       )
                .ToList();
半城柳色半声笛 2024-12-20 12:19:35

使用.GroupBy,您将获得IGrouping列表,因此选择如下所示:

IEnumerable<Reward> listConcat = list1.Concat(list2)
                                .GroupBy(x => x.User)
                                .Select(x => new Reward { User= x.Key, Money=x.Sum(y => y.Money) });

With .GroupBy, you get a list of IGrouping, so the select is like so:

IEnumerable<Reward> listConcat = list1.Concat(list2)
                                .GroupBy(x => x.User)
                                .Select(x => new Reward { User= x.Key, Money=x.Sum(y => y.Money) });
固执像三岁 2024-12-20 12:19:35

我想象一个接近您正在寻找的答案看起来像这样:

var catList = list1.Concat(list2)
                   .GroupBy (x => x.User)
                   .Select (y => new Reward{Game = 0, Money = y.Sum (z => z.Money), 
                                User =  y.Key})
                   .ToList();

至于要使用哪个游戏,您可能会考虑匿名选择(即

.Select(new {Money = y.Sum (z => z.Money), User =  y.Key})

这将为您提供一个匿名类型,其中包含您希望使用的信息。如果您是要将其传递出该方法,但是,最佳实践是为其指定类型,或者仅了解游戏 0 是所有游戏的超集。

I imagine an answer close to what you are looking for looks like this:

var catList = list1.Concat(list2)
                   .GroupBy (x => x.User)
                   .Select (y => new Reward{Game = 0, Money = y.Sum (z => z.Money), 
                                User =  y.Key})
                   .ToList();

As far as which game to use, you might consider an anonymous select (i.e.

.Select(new {Money = y.Sum (z => z.Money), User =  y.Key})

This would give you an anonymous type that holds the information you wish to use. If you are going to pass it out of the method, however, it's best practice to give it a type, or just understand that game 0 is your superset of all games.

鹿港巷口少年归 2024-12-20 12:19:35
IList<Reward> listConcat = list1.Concat(list2)
                .GroupBy(person => person.User)
                .Select(group => group.Aggregate((rewardone, rewardtwo) => rewardone.Merge(rewardtwo)))
                .ToList();
IList<Reward> listConcat = list1.Concat(list2)
                .GroupBy(person => person.User)
                .Select(group => group.Aggregate((rewardone, rewardtwo) => rewardone.Merge(rewardtwo)))
                .ToList();
十级心震 2024-12-20 12:19:35

请将行更改

 return new Reward { Game = this.Game, User = this.User, Money = this.Money + p.User

为:

 return new Reward { Game = this.Game, User = this.User, Money = this.Money + p.Money

Please change the line:

 return new Reward { Game = this.Game, User = this.User, Money = this.Money + p.User

to

 return new Reward { Game = this.Game, User = this.User, Money = this.Money + p.Money
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文