根据键c#合并两个列表

时间:2011-11-02 22:33:48

标签: c# list merge

我有课程奖励,我保持每场比赛的用户资金。 我有两个列表,我需要联系。我在下面写下样本列表和期望的结果。

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

但是如何?

6 个答案:

答案 0 :(得分:5)

你在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)
    });

答案 1 :(得分:0)

请更改行:

 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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

使用.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) });

答案 4 :(得分:0)

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

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是你所有游戏的超集。

答案 5 :(得分:0)

IList<Reward> listConcat = list1.Concat(list2)
                .GroupBy(person => person.User)
                .Select(group => group.Aggregate((rewardone, rewardtwo) => rewardone.Merge(rewardtwo)))
                .ToList();