Hello Ricardo good night.
Come on you must want it within the same correct query ?
for this you must use a ternary operator
await _context.Produtos
.Where(x => pesquisa.Nome != null ? x.Nome == pesquisa.Nome : false)
.Where(x => pesquisa.Material != null ? x.Material == pesquisa.Material : false)
.Where(x => pesquisa.Estado != null ? x.Estado == pesquisa.Estado : false)
.Where(x => pesquisa.PrecoInicial != null ? x.Preco >= pesquisa.PrecoInicial : false)
.Where(x => pesquisa.PrecoFinal != null ? x.Preco <= pesquisa.PrecoFinal : false)
.ToListAsync();
A bit about ternary operator with Master Macoratti
http://www.macoratti.net/14/11/c_tern1.htm
This way you will search for all attributes in the same query.
UPDATE
I apologize I had put a true at the end that picks null values from the table.
Use false that it will not fetch null values from the table.
Below is an example of the query generated in a table of mine with the true
.Where(x => pesquisa.PrecoInicial != null ? x.Preco >= pesquisa.PrecoInicial : true)
SELECT [a].[ViaturasID],[a].[ValorPrestacao]
FROM [Viaturas] AS [a]
WHERE ((@__prestacaoInicial_0 IS NOT NULL AND ([a].[ValorPrestacao] >= @__prestacaoInicial_1)) OR @__prestacaoInicial_0 IS NULL) AND ((@__prestacaoFinal_2 IS NOT NULL AND ([a].[ValorPrestacao] <= @__prestacaoFinal_3)) OR @__prestacaoFinal_2 IS NULL)
and with the false
.Where(x => pesquisa.PrecoInicial != null ? x.Preco >= pesquisa.PrecoInicial : false)
SELECT [a].[ViaturasID],[a].[ValorPrestacao]
FROM [Viaturas] AS [a]
WHERE (@__prestacaoInicial_0 IS NOT NULL AND ([a].[ValorPrestacao] >= @__prestacaoInicial_1)) AND (@__prestacaoFinal_2 IS NOT NULL AND ([a].[ValorPrestacao] <= @__prestacaoFinal_3))
================ NEW SOLUTION ================
Based on the comments of Virgilio Novic I really saw that this will make the consultation more effective.
Since this came all from the reply of Marcelo Uchimura.
IQueryable<Produtos> query = _context.Produtos;
if (pesquisa.Nome != null)
{
query = query.Where(x => x.Nome == pesquisa.Nome);
}
if (pesquisa.Material != null)
{
query = query.Where(x => x.Material == pesquisa.Material);
}
if (pesquisa.Estado != null)
{
query = query.Where(x => x.Estado == pesquisa.Estado);
}
if (pesquisa.PrecoInicial != null)
{
query = query.Where(x => x.Preco >= pesquisa.PrecoInicial);
}
if (pesquisa.PrecoFinal != null)
{
query = query.Where(x => x.Preco <= pesquisa.PrecoFinal);
}
query.ToList();
Example of query generated in a table of mine
SELECT [m].[ViaturasID],[m].[ValorPrestacao]
FROM [Viaturas] AS [m]
WHERE ([m].[ValorPrestacao] >= @__prestacaoInicial_0) AND ([m].[ValorPrestacao] <= @__prestacaoFinal_1)
If you want to use . Tolistasync() change the ending to
await query.ToListAsync();
Sincerity I work with EF for a long time and what is explained above does not make much sense, I wanted to really understand before putting a plausible opinion, since Tolistasync at the end of the code will not have the later filters! The most correct answer is that of Marcelo who took negative votes without need (perhaps because of Ienumerable, maybe)
– novic
I questioned the
ToListAsync()
, I just left what he’s doing. In fact I forgot to put the assignment. Good you questioned why I hadn’t noticed. That of Marcelo usesHasValue
in fields that are clearly by reference, and have a method that only he knows what he does, and the error you pointed out. then I don’t know how this can be right.– Maniero
Good
_context
would not be a legal assignment, is the variableDBContext
, should be made for another variable by question. I can agree that there are some points that improve the response of the friend, but, in compensation to his before was the closest to reality, including Maniero, has duplicate, I’ve seen, but, I can not find by the search system.– novic
I think you’re right. If there is something else you saw wrong I improve. Thank you for the questions, only contributed to a better result for all. Really the search is bad ,if you find brand there I finish closing.
– Maniero
Maniero for this your function work you have to change from Iqueryable<Product> to Iqueryable<Products> and change from query.Product.Where to query. Where later in subsequent query.
– Luciano Oiticica Lemgruber
@Lucianooiticicalemgruber yes, I had these mistakes and I corrected now, thank you, I recognize my mistakes and who helps me. I just don’t understand why the same mistake in another answer gets congratulated.
– Maniero