4

我已经玩了一段时间了,只是无法理解。我是 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 将其添加到该组?

编辑:我需要将结果放入列表,因为我还需要处理其他事情......

谢谢!

4

3 回答 3

2

Try this:

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));
            }
        }
于 2012-06-06T16:20:34.957 回答
1

试试下面的 GroupBy 表达式:

.GroupBy(m => Math.Min(m.UserId, m.UserIdTo) + ',' + Math.Max(m.UserId, m.UserIdTo))
于 2012-06-06T15:42:15.377 回答
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 值。

于 2012-06-06T16:37:36.500 回答