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?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.