LINQ和Expression的最高价格

时间:2016-08-24 19:22:25

标签: c# linq expression

我有GenericRepository方法

public virtual IEnumerable<tentity> Get(
Expression<func<tentity, bool="">> filter = null,
Func<iqueryable<tentity>, IOrderedQueryable<tentity>> orderBy = null,
string includeProperties = "")
{
  IQueryable<tentity> query = dbSet;
  if (filter != null)
  {
    query = query.Where(filter);
  }
  foreach (var includeProperty in includeProperties.Split
      (new char[] { ’,’ }, StringSplitOptions.RemoveEmptyEntries))
  {
    query = query.Include(includeProperty);
  }
  if (orderBy != null)
  {
    return orderBy(query).ToList();
  }
  else
  {
    return query.ToList();
  }
}

例如,我得到了产品:

var products = _database.ProductRepository.Get(
filter: d => d.Price == price.Value,
orderBy:
q => q.OrderBy(d => d.Name));

如何获得最高价格的产品? (可能像d.Max(d => d.Price)

1 个答案:

答案 0 :(得分:1)

尝试使用LINQ:

var product = from p in products
              where p.Price == products.Max(s => s.Price)
              select p;