Error when using GROUP via LINQ

Asked

Viewed 104 times

0

I’m trying to perform the query below:

                var query = from s in db.Crm_Analise
                        where s.cliente_CRM == cod_cli
                        group s by s.TAG into g
                        select new
                        {
                            TAG = g.Key,
                            ATUALIZAÇÃO = g.Max(t => t.data_creat),
                            RELATÓRIOS = g.Count(t => t.modelo != null)
                        };

However, the error is presented:

inserir a descrição da imagem aqui

Follows the View:

   @model IEnumerable<OneeWeb_v3.Models.Crm_Analise>
@{
    ViewBag.title = "TAG's";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>    
</head>
<body>
    <h2>@ViewBag.title</h2> 

    <table class="table">
        <tr>          
            <th>
                @Html.ActionLink("TAG", "Index")
            </th>
            <th>
                @Html.ActionLink("ATUALIZAÇÃO", "Index")
            </th>
            <th>
                @Html.ActionLink("RELATÓRIOS", "Index")
            </th>
            <th></th>
        </tr>

    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.TAG)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.data_creat)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.modelo)
            </td>
            <td>
                @Html.ActionLink("Visualizar", "Details") 
            </td>
        </tr>
    }
</table>

<ul class="pager">
    <li><a href="#">Anterior</a></li>
    <li><a href="#">Proximo</a></li>
</ul>

Model:

 public class Crm_Analise
{
    [Key]
    public int id { get; set; }
    public string cod_item_CRM { get; set; }
    public string TAG { get; set; }
    public string data_creat { get; set; }
    public string modelo { get; set; }   
    public int cliente_CRM { get; set; }
}

Controller:

        // GET: Crm_Analise
    [Authorize]
    public async Task<ActionResult> Index()
    {
        if (Session["cod_cli"] != null)
        {
            int cod_cli = Convert.ToInt32(Session["cod_cli"]);

            var query = from s in db.Crm_Analise
                        where s.cliente_CRM == cod_cli
                        select new
                        {
                            TAG = s.TAG,
                            ATUALIZAÇÃO = s.data_creat,
                            RELATÓRIOS = s.cod_item_CRM
                        };

            if (query.Count() > 0)
            {
                return View(await query.ToListAsync());
            }
            else
            {
                return RedirectToAction("Index", "Home", new { cod_cli = Session["cod_cli"], razao_social = Session["razao_social"] });
            }

        }
        else
        {
            return RedirectToAction("Login", "Account");
        }
    }

I know it has something to do with @model IEnumerable<OneeWeb_v3.Models.Crm_Analise> but I don’t know how to solve..

  • 1

    the type sent to View is different from what is in your configuration, you need to create a ViewModel when that’s how

  • @Virgilionovic Thanks. I will search on viewmodel

  • 1

    Dude, put the method that you encapsulate to the screen object and send the controller as well to help... Probably you are sending an object of type "A", but on your screen the "@model" expects the type B.

  • 1

    Example: https://answall.com/questions/169742/onde-coloca-a-regra-da-view-model/169761#169761

  • @Ayrtongiffoni Update made. thank you

1 answer

2


try changing your select in the query

var query = from s in db.Crm_Analise
    where s.cliente_CRM == cod_cli
    select new Crm_Analise()
    {
        TAG = s.TAG,
        data_creat = s.data_creat,
        cod_item_CRM = s.cod_item_CRM
    };

also change the head of your table so that it does not need to go in the controller to get the name (do this only if necessary)

<table class="table">
    <thead>
        <tr>          
            <th>
                TAG
            </th>
            <th>
                Atualização
            </th>
            <th>
                Relatórios
            </th>
            <th></th>
        </tr>

    </thead>
    <tbody>
        @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.TAG)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.data_creat)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.modelo)
                </td>
                <td>
                    @Html.ActionLink("Visualizar", "Details") 
                </td>
            </tr>
        }
    </tbody>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.