在EntityFramework中进行分组 [英] Take and Group By in EntityFramework

查看:222
本文介绍了在EntityFramework中进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从对话中获取(例如2条)2条消息

I need to take (for example, 2), 2 messages from a conversation

示例:

id = idConversation

id = idConversation

Id | messageId | Message
---|-----------|--------
1  | 1         | "asd"
1  | 2         | "asd2"
1  | 3         | "asd3"
1  | 4         | "asd4"
2  | 5         | "asd5"
3  | 6         | "asd6"
3  | 7         | "asd7"
3  | 8         | "asd8"
3  | 9         | "asd9"
3  | 10        | "asd10"
4  | 11        | "asd11"
4  | 12        | "asd12"
4  | 13        | "asd13"

我想要那个

Id   messageId   Message
---|-----------|--------
1  | 1         | "asd"
1  | 2         | "asd2"
2  | 5         | "asd5"
3  | 6         | "asd6"
3  | 7         | "asd7"
4  | 11        | "asd11"
4  | 12        | "asd12"

我可以进行grouby idConversation,但是我不能在对话中使用grouby限制数量.

i can grouby idConversation, but i cant limit quantity using grouby in a conversation.

var test = unitOfWork.ChatMensagemRepository.GetAll()
                  .Where(x => x.PessoaCodigoPessoa == codigoRemetente)
                  .GroupBy(x => x.ChatConversaCodigoChatConversa)
                  .Select(group => new
                  {
                      codigoChat = group.Key,
                      list = group.Select(mensagem => new
                      {
                          // do stuff
                      })
                  }).ToList();

这没关系...但是当我进行group.take(2).Select .....时,不要限制我的列表. 给我子查询返回多于1行"

this is ok... but dont limit my list, when i do group.take(2).Select..... give me "Subquery returns more than 1 row"

var test = unitOfWork.ChatMensagemRepository.GetAll()
                  .Where(x => x.PessoaCodigoPessoa == codigoRemetente)
                  .GroupBy(x => x.ChatConversaCodigoChatConversa)
                  .Select(group => new
                  {
                      codigoChat = group.Key,
                      list = group.Take(2).Select(mensagem => new
                      {
                          // do stuff
                      })
                  }).ToList();

错误:子查询返回多于1行

error : Subquery returns more than 1 row

var test = unitOfWork.ChatMensagemRepository.GetAll()
                  .Where(x => x.PessoaCodigoPessoa == codigoRemetente)
                  .GroupBy(x => x.ChatConversaCodigoChatConversa)
                  .Select(group => new
                  {
                      codigoChat = group.Key,
                      list = group.Select(mensagem => new
                      {
                          // do stuff
                      }).take(2)
                  }).ToList();

错误:子查询返回多于1行

error : Subquery returns more than 1 row

推荐答案

我尝试了此代码,并且工作正常:

I tried this code and is working fine:

class Conversation
{
    public int Id;
    public string Message;
    public int MessageId;
}

class Program
{

    static void Main(string[] args)
    {
        var inputList = new List<Conversation>
        {
            new Conversation() {Id = 1, Message = "asd0", MessageId = 1},
            new Conversation() {Id = 1, Message = "asd1", MessageId = 2},
            new Conversation() {Id = 1, Message = "asd2", MessageId = 3},
            new Conversation() {Id = 2, Message = "asd3", MessageId = 4},
            new Conversation() {Id = 2, Message = "asd4", MessageId = 5},
            new Conversation() {Id = 2, Message = "asd5", MessageId = 6},
            new Conversation() {Id = 3, Message = "asd6", MessageId = 7}
        };

        var outputList = inputList.GroupBy(x => x.Id)
            .SelectMany(x => x.OrderBy(y => y.MessageId).Take(2).ToList())
            .ToList();

        Console.ReadKey();
    }
}

这篇关于在EntityFramework中进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆