在EntityFramework中获取和分组

时间:2017-08-21 14:12:48

标签: c# mysql entity-framework linq

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

示例:

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"

我可以通过idConversation,但是我不能在对话中使用grouby来限制数量。

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)时,不要限制我的清单。选择..... 给我"子查询返回超过1行"

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行

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行

2 个答案:

答案 0 :(得分:0)

这是我认为你所追求的一个例子。此查询将返回每个类中按GPA排名前3位的数据:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ef6Test
{

    class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }

        public string Class{ get; set; }

        public decimal GPA { get; set; }
    }

    class Db : DbContext
    {
        public DbSet<Student> Students { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            using (var db = new Db())
            {
                var q = db.Students
                         .GroupBy(s => s.Class)
                         .SelectMany(g => g.OrderByDescending(s => s.GPA).Take(3));

                Console.WriteLine(q.ToString());

                Console.ReadKey();
            }

        }
    }
}

答案 1 :(得分:0)

我尝试了这段代码并且工作正常:

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