简体   繁体   English

按ID分组并使用Linq显示前1个

[英]Grouping by ID and displaying the top 1 with Linq

Looking for a bit of advice on grouping with entity framework and linq. 在与实体框架和linq分组方面寻求一些建议。

So i have a table "tbl_ChatLog" that contains userID and sendToUserID etc...and with this data i'm trying to display the "Top 1" from each "SendToUserID" 所以我有一个表“ tbl_ChatLog”,其中包含用户ID和sendToUserID等...,并以此数据我试图显示每个“ SendToUserID”中的“ Top 1”

So in my UI it would look like so: 所以在我的用户界面中,它看起来像这样:

  • 1001 (Contains multiple but show the top 1) 1001(包含多个,但显示前1个)
  • 1003 (Contains multiple but show the top 1) 1003(包含多个,但显示前1个)
  • 1008 (Contains multiple but show the top 1) 1008(包含多个,但显示前1个)
  • 1009 (Contains multiple but show the top 1) 1009(包含多个,但显示前1个)

The start of my code below: 我的代码如下:

public static List<Chat> getChatMessage() 
        {
            var entities = new FreeEntities();
            //My ID
            Business.User user = Business.User.getUserBySecurityToken();

             List<Chat> chatMessages = 
             (
                from cm in entities.tbl_ChatLog 
                where cm.UserID == user.uid 
                select new Chat 
                { 
                   uid = (int)cm.UserID, 
                   sendToUserID = (int)cm.SendToUserID, 
                   message = cm.Message, dateAdded = (DateTime)cm.DateAdded 
                }
             ).OrderByDescending(x => x.dateAdded).ToList();
            return chatMessages;
        }

Hoping you can help me out on this one. 希望你能帮我这个忙。 Grouping always seems to throw me. 分组似乎总是让我失望。

Much appreciated, 非常感激,

Terry 特里

You could use groupby : 您可以使用groupby

List<Chat> chatMessages =

(from cm in entities.tbl_ChatLog 

where cm.UserID == user.uid 

orderby cm.DateAdded descending

group cm by cm.sendToUserID into grp

select new Chat
{ 

uid = grp.FirstOrDefault().UserID,

sendToUserID = grp.Key, 

message = grp.FirstOrDefault().Message,

dateAdded = grp.FirstOrDefault().DateAdded

}).ToList()

This will get you a list of your table data grouped by sendToUserID and the first entry of each group containing every property including sendToUserID . 这将为您提供按sendToUserID分组的表数据列表,以及每个组的第一个条目,其中包含每个属性,包括sendToUserID

The original problem was trying to select the first message from each unique "SendToUserId" and grouping by UserId and then selecting into a DTO was a nightmare but i managed to get a simple work around. 最初的问题是尝试从每个唯一的“ SendToUserId”中选择第一条消息并按UserId分组,然后选择进入DTO是一场噩梦,但我设法解决了一个简单的问题。 Code below: 代码如下:

getFirstChatMessage() 
    {
        var entities = new FreeEntities();
        //User ID
        Business.User user = Business.User.getUserBySecurityToken();

        // Create new list of Chat
        List<Chat> chatList = new List<Chat>();

          var res = from c in entities.tbl_ChatLog
          where c.UserID == user.uid
          group c by c.UserID
          into groups
          select groups.OrderByDescending(p => p.DateAdded).FirstOrDefault();

        foreach (var r in res) 
        {
          chatList.Add(new Chat() 
          {
           uid = (int)r.UserID, 
           sendToUserID = (int)r.SendToUserID,
           message = (from m in entities.tbl_ChatLog where m.UserID == (int)r.SendToUserID 
           orderby r.DateAdded descending select m.Message).FirstOrDefault(),
           dateAdded = (DateTime)r.DateAdded, 
           fullName = (from b in entities.tbl_Bio where b.UserID == (int)r.SendToUserID 
           select b.FirstName + " " +      b.SurName).FirstOrDefault() 
          });
        }
        return chatList;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM