Reputation: 25
I have this sentence in sql server:
SELECT * FROM Documentos WHERE ( @param IS NULL OR strNOMPRO = @param )
and I do this with entity
DocumentsList = db.DOCUMENTOS
.Where(d => d.strNOMPRO == nombre && d.strNOMPRO == null)
.OrderByDescending(d => d.datFECCER).ToList();
if param is null this return all the registers in sql but in linq return 0 registers
How i can do this well?
Upvotes: 2
Views: 151
Reputation: 236308
This is an equivalent of your SQL WHERE statement:
DocumentsList = db.DOCUMENTOS
.Where(d => nomber == null || d.strNOMPRO == nombre)
.OrderByDescending(d => d.datFECCER)
.ToList();
It checks parameter for null
OR (|| Operator) it checks that strNOMPRO property should be equal to parameter.
BTW thus your parameter cannot change in the middle of query, it's more efficient to add filtering condition dynamically:
var query = db.DOCUMENTOS;
if (!String.IsNullOrEmpty(nombre))
query = query.Where(d => d.strNOMPRO == nombre);
DocumentsList = query.OrderByDescending(d => d.datFECCER).ToList();
Further readings: && Operator and || Operator
Upvotes: 5