I've got a LINQ query to return lookup data. A bool parameter is passed in called active.
I want to either return only the rows where active = true
OR
return all rows regardless of active status
Is there an elegant way to do this in the where clause of LINQ?
return await (from p in db.PackSizeTypes.AsNoTracking()
where p.Active == true
orderby p.ID
select new PackSizeTypeObject
{
ID = p.ID,
Name = p.Name.Trim(),
LastEditedDate = p.LastEditedDate,
LastEditedBy = p.LastEditedBy,
Active = p.Active
}).ToListAsync();
IEnumerable<PackSizeTypeObject> MyPackSizeTypeObjects(bool? isActive)
{
return await (from p in db.PackSizeTypes.AsNoTracking()
where p.Active == (isActive == null ? p.Active : isActive)
orderby p.ID
select new PackSizeTypeObject
{
ID = p.ID,
Name = p.Name.Trim(),
LastEditedDate = p.LastEditedDate,
LastEditedBy = p.LastEditedBy,
Active = p.Active
}).ToListAsync();
}