1
I have the following tables in my base:
Produto
Id | Nome | Preco
1 | fone | 100
2 | copo | 50
3 | pao | 10
ProdutoStatus
IdProduto | Versao | Preco
1 | 1 | 101
1 | 2 | 110
1 | 3 | 90
2 | 1 | 55
2 | 2 | 60
3 | 3 | 5
And I need to make a method that searches only the most current versions with their due prices of each product
I am using Entity Framework Core to access this base, already with the mapping of tables implemented.
var data = _context.Produto
.Include(u => u.ProdutoStatus)
.Where(x => x.id == x.ProdutoStatus.OrderByDescending(a => a.Versao).FirstOrDefault().IdProduto).ToList()
This code above brings all products and all versions
someone can help me?
EDIT
Look at the entities in these tables
public class Produto{
public int Id {get; set;}
public string Nome {get; set;}
public int Preco {get; set;}
public virtual ICollection<ProdutoStatus> ProdutoStatus {get; set;}
}
public class ProdutoStatus{
public int IdProduto {get; set;}
public int Versao {get; set;}
public int Preco {get; set;}
public virtual Produto Produto {get; set;}
}
Important to Know
- I don’t have access to change the tables
- I used an example similar to the one I have
thanks for the reply, but in this Groupby, I do not have access to the internal properties of the Productostatus Collection, I made an Edit with the definition of Entities.
– Guilherme Golfetto
a doubt, you want only the list of
ProductStatus
or eitherProduct.ProductStatus
where there is only 1 item with the latest version?– Ricardo Pontual
only Productstatus with the most current version of all Products
– Guilherme Golfetto
var result = _context.Produto
 .Select(p => p.ProdutoStatus.OrderByDescending(o => o.Versao).First())
 .ToList();
That doesn’t solve it then?– Ricardo Pontual
This LINQ makes a lot of sense, however in the search returns a null line, trying to understand why the foreign key is correct. It makes some correlation between the Products Table and the Productostatus besides the key ?
– Guilherme Golfetto