Take(limit) list inside Groupby in Entity Framework

久未见 提交于 2020-01-11 07:05:48

问题


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

I dont care about how my list looks like, but i want only 2 messages from id 1, 2 messages from id2, and go on

example:

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"

and i want that

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

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

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

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

error : Subquery returns more than 1 row


回答1:


It is caused, because EF provider for MySQL or server itself can't translate this linq to SQL, so you should at first get data from server and only then group it with Take(2):

var test = unitOfWork.ChatMensagemRepository.GetAll()
              .Where(x => x.PessoaCodigoPessoa == codigoRemetente)
              //this section is added
              .Select(x => new 
              {
                  x.ChatConversaCodigoChatConversa,
                  x.prop1,//specify only columns, which you need for below code with Take
                  x.prop2
              }).ToList()
              //end of section
              .GroupBy(x => x.ChatConversaCodigoChatConversa)
              .Select(group => new
              {
                  codigoChat = group.Key,
                  list = group.Take(2).Select(mensagem => new
                  {
                     mensagem.prop1, 
                     mensagem.prop2
                  }).ToList()
              }).ToList();


来源:https://stackoverflow.com/questions/45837750/takelimit-list-inside-groupby-in-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!