LINQ Return from the list is with all the repeated records

Asked

Viewed 739 times

0

I’m having problems performing a select (using LINQ) in a View in SQL Server 2012. The values stored in the bank are as below:

   ID_Acomp    ID_Pessoa    Nome      Data
     26           300     MONTEIRO  01-01-2016
     27           300     MONTEIRO  02-02-2016
     28           300     MONTEIRO  03-03-2016

When I perform select in SQL Manager, the values return perfectly. But when I do the same select through LINQ, the value of the last record is replicated over the other records above, and it looks like this:

   ID_Acomp    ID_Pessoa    Nome      Data
     28           300     MONTEIRO  03-03-2016
     28           300     MONTEIRO  03-03-2016
     28           300     MONTEIRO  03-03-2016

The code I’m using in the application basically is this:

IQueryable<VW_PESSOA_ACOMPANHAMENTO> vwPessoaAcomp =  
                            contexto.VW_PESSOA_ACOMPANHAMENTO.AsQueryable();

if (ID_Pessoa > 0)
{
    vwPessoaAcomp = vwPessoaAcomp(p => p.ID_Pessoa == ID_Pessoa);
}

var retorno = (from A in vwPessoaAcomp
                 orderby A.ID_Acomp descending
                   select A).ToList();

Below is the code of my view:

SELECT A.ID_Acomp, P.ID_Pessoa, P.Nome, A.Data 
    FROM ACOMPANHAMENTO A, PESSOA P WHERE A.ID_ACOMP = P.ID_ACOMP
  • is mysql? which SQL is running in SQL Manager? if you can put the question!

  • Using SQL Server 2012

  • Which SQL you use in SQL Manager?

  • Enter the code of your view, you post the select of sql. This will facilitate the help. You debugged the "return" to see if you generated the records the way you would like ?

  • The debug of the return from Linq yes, but I never debug in sql manager. When I run the select from the view in sql manager the return is correct.

  • You are using the Entity framework?

Show 1 more comment

1 answer

0

The code below should work perfectly, if it is returning wrong probably the problem is in the View, in the tables involved, or in the data involved:

var vwPessoaAcomp = contexto.VW_PESSOA_ACOMPANHAMENTO.AsQueryable();

if (ID_Pessoa > 0)
{
    vwPessoaAcomp = vwPessoaAcomp.Where(p => p.ID_Pessoa == ID_Pessoa);
}

var retorno = vwPessoa.OrderBy(p => p.ID_Acomp).ToList();

I would modify the view code to the following code, which I consider more readable:

SELECT A.ID_Acomp, P.ID_Pessoa, P.Nome, A.Data 
    FROM ACOMPANHAMENTO A JOIN PESSOA P ON A.ID_ACOMP = P.ID_ACOMP

You could give a SELECT * FROM PESSOA and another SELECT * FROM ACOMPANHAMENTO and publish the results here to know what data are involved in this case.

  • The problem is that when running the view, the data is being displayed perfectly.

  • @The error is not in LINQ, it is in another point of your code. Can you create a simple, full console program that reproduces the error? How to also display your query data? Are you using Entity Framework or LINQ to SQL? Try to see which SQL is being issued to retrieve the data in the database. It is even possible that your display routine is doing something wrong. By the example you are giving it is impossible to guess where the problem is.

Browser other questions tagged

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