I am new to using generics. Very powerful stuff but a bit confusing to me and I'm trying to understand how to use the following code:
public async Task<IEnumerable<TEntity>> Find(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderExpression = null)
{
try
{
return await this.GetQuery(predicate, orderExpression);
}
catch (Exception e)
{
return null;
}
}
public async Task<IQueryable<TEntity>> GetQuery(Expression<Func<TEntity, bool>> predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderExpression = null)
{
IQueryable<TEntity> qry = this.DBSet;
if (predicate != null)
qry = qry.Where(predicate);
if (orderExpression != null)
return orderExpression(qry);
return qry;
}
I am able to call the find method with a predicate like so:
@code {
[Parameter]
public string CurrentID { get; set; }
Repository2<User> repository2 = new Repository2<User>();
IEnumerable<User> users;
User user = new User();
protected override async Task OnInitializedAsync()
{
users = await Task.Run(() => repository2.Find(u => u.ID == Convert.ToInt32(CurrentID)));
user = users.SingleOrDefault();
}
protected void UpdateUser()
{
repository2.Update<User>(user);
repository2.SaveChanges();
navidationMananger.NavigateTo("Users");
}
void Cancel()
{
navidationMananger.NavigateTo("Users");
}
}
However I am unable to figure out how use the OrderBy functionality of the above Find method.
How would I call this method passing an order by as the second parameter? If you could explain it a bit so I understand why it works that would be most appreciated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…