public IEnumerable<ProductCardSearchViewModel>IEnumerable<ProductCardRowViewModel> Search() { GenericRepository<Rows> repo = new GenericRepository<Rows>(); var temp = repo.GetAll(); if (this.Warehouse != 0) temp = temp.Where(x => x.Head.Warehouse == this.Warehouse ); if (ProductCode > 0) temp = temp.Where(x => x.ProductCode == this.ProductCode ); if (!string.IsNullOrEmpty(this.UnitCode)) temp = temp.Where(x => x.UnitCode .Equals(this.UnitCode)); List<ProductCardSearchViewModel>List<ProductCardRowViewModel> data = new List<ProductCardSearchViewModel>List<ProductCardRowViewModel>(); foreach (Rows row in temp) data.Add(new ProductCardSearchViewModelProductCardRowViewModel(row)); return data; }
omitting braces
{}in places where they are optional is bad practice and leads to error prone code.in its current state this method is returning for
Warehouse == 0,ProductCode == 0and for instanceUnitCode == string.Emptyall items. If this is what you want then IMO a comment should describe this behaviour.you are sometimes using
thisand sometimes you don't. Choose one style and stick to it.I would add a
ToList()totempin the loop. And by the waytempis a really bad name.instead of building the returned value as a
List<T>you could simply useyield return. If you want to keep theListyou should at least usevar.
Applying the mentioned points would lead to
public IEnumerable<ProductCardSearchViewModel>IEnumerable<ProductCardRowViewModel> Search()
{
GenericRepository<Rows> repo = new GenericRepository<Rows>();
var rows = repo.GetAll();
if (this.Warehouse != 0)
{
rows = rows.Where(x => x.Head.Warehouse == this.Warehouse );
}
if (this.ProductCode > 0)
{
rows = rows.Where(x => x.ProductCode == this.ProductCode );
}
if (!string.IsNullOrEmpty(this.UnitCode))
{
rows = rows.Where(x => x.UnitCode.Equals(this.UnitCode));
}
foreach (Rows row in rows.ToList())
{
yield return new ProductCardSearchViewModelProductCardRowViewModel(row);
}
}