C#linq group by on同一实体中的不同键

时间:2012-06-06 15:37:23

标签: c# asp.net linq lambda

我已经玩弄了一段时间而且无法得到它。我是Linq,C#以及这些Lambda的新手。

我想要做的是根据每个实体的两个属性对实体进行分组。它是一个消息实体:

Message
{
int UserId; //The user generating the message
int UserIdTo; //The receiver of the message
|...| // Other stuff 
}

所以,我想要它,以便这些UserId = 5,UserIdTo = 6和UserId = 6,UserIdTo = 5将在同一组中。

这是我的开始:

var groupList = (from m in db.Messages
                             where m.UserId == userId || m.UserIdTo == userId
                             join u in db.Users on m.UserId equals u.UserId
                             join w in db.Users on m.UserIdTo equals w.UserId
                             orderby m.MessageTimestamp descending
                             select new DomMessage
                             {
                                 MessageId = m.MessageId,
                                 MessageContent = m.MessageContent,
                                 MessageTimestamp = m.MessageTimestamp,
                                 UserId = m.UserId,
                                 UserIdTo = m.UserIdTo,
                                 ScreenName = u.ScreenName,
                                 ScreenName2 = w.ScreenName
                             }).GroupBy(m=>m.UserId == userId)
                             .ToList();

这是UserId的第一个分组,但是我一直试图扩展它,以便结果组中的任何UserId值等于其他地方的UserIdTo将其添加到该组中?

编辑:我需要将结果转到List,因为还有其他我需要做的事情......

谢谢!

3 个答案:

答案 0 :(得分:2)

试试这个:

var payload = new[] 
        {
            new{ To = 1, From = 2, Message = "msj1" },
            new{ To = 1, From = 2, Message = "msj2" },
            new{ To = 2, From = 1, Message = "msj3" },
            new{ To = 4, From = 1, Message = "msj4" },
            new{ To = 1, From = 3, Message = "msj5" }
        };


        var groupped = payload.Select(x => new { Key = Math.Min(x.To, x.From) + "_" + Math.Max(x.To, x.From), Envelope = x }).GroupBy(y => y.Key).ToList();

        foreach (var item in groupped)
        {
            Console.WriteLine(String.Format(@"Group: {0}, messages:", item.Key));

            foreach (var element in item)
            {
                Console.WriteLine(String.Format(@"From: {0} To: {1} Message: {2}", element.Envelope.From, element.Envelope.To, element.Envelope.Message));
            }
        }

答案 1 :(得分:1)

尝试以下GroupBy表达式:

.GroupBy(m => Math.Min(m.UserId, m.UserIdTo) + ',' + Math.Max(m.UserId, m.UserIdTo))

答案 2 :(得分:0)

我认为这是最简单的方法:

var groupList = from a in (
from m in db.Messages
where m.UserId == userId || m.UserIdTo == userId
join u in db.Users on m.UserId equals u.UserId
join w in db.Users on m.UserIdTo equals w.UserId
select new 
{
 MessageId = m.MessageId,
 MessageContent = m.MessageContent,
 MessageTimestamp = m.MessageTimestamp,
 UserId = m.UserId,
 UserIdTo = m.UserIdTo,
 ScreenName = u.ScreenName,
 ScreenName2 = w.ScreenName
})
group a by new { 
    UserId = a.UserId,
     UserIdTo = a.UserIdTo
    } into grp
orderby grp.Max(a => a.MessageTimestamp) descending
select new
{
 UserId = grp.Key.UserId
 UserIdTo = grp.Key.UserIdTo,
 MessageId = grp.Max(a => a.MessageId),
 MessageContent = grp.Max(a => a.MessageContent),
 MessageTimestamp = grp.Max(a => a.MessageTimestamp),
 ScreenName = grp.Max(a => a.ScreenName),
 ScreenName2 = grp.Max(a => a.ScreenName2)
}

您必须告诉它如何处理您没有分组的字段。在这种情况下,我得到每个的MAX值。