private IEnumerable<LivrosEmprestadosPorPeriodo> LivrosAtrasadosPorPeriodoUsuario(RelatorioEmprestimo relatorioEmprestimo)
 {
     relatorioEmprestimo.DataAte = relatorioEmprestimo.DataAte.GetValueOrDefault().AddDays(1).AddMilliseconds(-1);
     return (from emprestimo in db.Emprestimo
             where emprestimo.MatriculaUsuario == relatorioEmprestimo.MatriculaUsuario &&
                   (emprestimo.DataEmprestimo >= relatorioEmprestimo.DataDe ||
                   emprestimo.DataEmprestimo <= relatorioEmprestimo.DataAte) &&
                   ((emprestimo.DataDevolucao == null && emprestimo.DataPrevista < DateTime.Now) ||
                   (emprestimo.DataDevolucao > emprestimo.DataPrevista))
             select new LivrosEmprestadosPorPeriodo
             {
                 DataEmprestimo = emprestimo.DataEmprestimo,
                 NomeLivro = emprestimo.Exemplar.Livro.Titulo + " - " + emprestimo.Exemplar.Edicao,
                 NomeUsuario = emprestimo.MatriculaUsuario + " - " + emprestimo.Usuario.Nome
             });
 }
 private IEnumerable<LivrosEmprestadosPorPeriodo> LivrosAtrasadosPorUsuario(RelatorioEmprestimo relatorioEmprestimo)
 {
     return (from emprestimo in db.Emprestimo
             where emprestimo.MatriculaUsuario == relatorioEmprestimo.MatriculaUsuario &&
                   ((emprestimo.DataDevolucao == null && emprestimo.DataPrevista < DateTime.Now) ||
                   (emprestimo.DataDevolucao > emprestimo.DataPrevista))
             select new LivrosEmprestadosPorPeriodo
             {
                 DataEmprestimo = emprestimo.DataEmprestimo,
                 NomeLivro = emprestimo.Exemplar.Livro.Titulo + " - " + emprestimo.Exemplar.Edicao,
                 NomeUsuario = emprestimo.MatriculaUsuario + " - " + emprestimo.Usuario.Nome
             });
 }
        private IEnumerable<LivrosEmprestadosPorPeriodo> TopDezLivrosPorPeriodo(RelatorioEmprestimo relatorioEmprestimo)
        {
            relatorioEmprestimo.DataAte = relatorioEmprestimo.DataAte.GetValueOrDefault().AddDays(1).AddMilliseconds(-1);
            var livros = (from emprestimo in db.Emprestimo
                          join livro in db.Livro
                            on emprestimo.IdLivro equals livro.Id
                          where (emprestimo.DataEmprestimo >= relatorioEmprestimo.DataDe ||
                                  emprestimo.DataEmprestimo <= relatorioEmprestimo.DataAte)
                          group emprestimo by new { emprestimo.IdLivro, livro.Titulo }
                          into grupo
                          select new { grupo, count = grupo.Count() }).OrderByDescending(x => x.count).ToList();

            return livros.Take(10).Select(x => new LivrosEmprestadosPorPeriodo
            {
                NomeLivro = x.grupo.Key.Titulo,
                Quantidade = x.count
            }).ToList();

        }
        private IEnumerable<LivrosEmprestadosPorPeriodo> TopUsuarioLivroPorPeriodo(RelatorioEmprestimo relatorioEmprestimo)
        {
            relatorioEmprestimo.DataAte = relatorioEmprestimo.DataAte.GetValueOrDefault().AddDays(1).AddMilliseconds(-1);
            var topUsuarios = (from emprestimo in db.Emprestimo
                               join usuario in db.Usuario
                                 on emprestimo.MatriculaUsuario equals usuario.Matricula
                               where (emprestimo.DataEmprestimo >= relatorioEmprestimo.DataDe ||
                                       emprestimo.DataEmprestimo <= relatorioEmprestimo.DataAte)
                               group emprestimo by new { usuario.Matricula, usuario.Nome }
                          into grupo
                               select new { grupo, count = grupo.Count() }).OrderByDescending(x => x.count).ToList();

            return topUsuarios.Select(x => new LivrosEmprestadosPorPeriodo
            {
                NomeUsuario = x.grupo.Key.Matricula + " - " + x.grupo.Key.Nome,
                Quantidade = x.count
            }).ToList();

        }
 private IEnumerable<LivrosEmprestadosPorPeriodo> LivrosEmprestadosPorPeriodo(RelatorioEmprestimo relatorioEmprestimo)
 {
     relatorioEmprestimo.DataAte = relatorioEmprestimo.DataAte.GetValueOrDefault().AddDays(1).AddMilliseconds(-1);
     return (from emprestimo in db.Emprestimo
             where (emprestimo.DataEmprestimo >= relatorioEmprestimo.DataDe ||
                   emprestimo.DataEmprestimo <= relatorioEmprestimo.DataAte)
             select new LivrosEmprestadosPorPeriodo
             {
                 DataEmprestimo = emprestimo.DataEmprestimo,
                 NomeLivro = emprestimo.Exemplar.Livro.Titulo + " - " + emprestimo.Exemplar.Edicao,
                 NomeUsuario = emprestimo.MatriculaUsuario + " - " + emprestimo.Usuario.Nome
             });
 }