CodeLiturgy.Dashboard/BlueWest.Api/Context/Templates/GetManyTemplate.csx

32 lines
1.3 KiB
Plaintext
Raw Normal View History

2022-09-06 00:42:45 +03:00
/// <summary>
/// Gets {propertyName}.
/// </summary>
/// <param name="dbContext">The database context.</param>
/// <param name="skip">How many {propertyName} to skip.</param>
/// <param name="take">How many {propertyName} to take. (Default = 50)</param>
/// <param name="orderDir">Optional Order direction</param>
/// <param name="where">Optional where predicate.</param>
/// <param name="orderBy">Optional order by predicate.</param>
2022-09-09 06:26:16 +03:00
/// <returns>A bool if the result is successful and a projection of the first occurrence of {propertyName}. </returns>
2022-09-06 00:42:45 +03:00
public static (bool, {returnTypeFullName}[]) Get{propertyName}(this {contextFullName} dbContext, int skip = 0, int take = 50, int orderDir = 1,
Expression <Func<{returnTypeFullName}, bool> > where = null,
Expression <Func<{returnTypeFullName}, object> > orderBy = null)
{
2022-09-09 06:44:33 +03:00
if (take > 200) take = 200;
2022-09-06 00:42:45 +03:00
var query = dbContext
.{propertyName}
.Select(x => new {returnTypeFullName}(x));
2022-09-09 06:44:33 +03:00
.Skip(skip)
.Take(take);
if (where != null) query = query.Where(where);
2022-09-06 07:54:48 +03:00
if(orderBy != null)
{
2022-09-09 06:44:33 +03:00
if (orderDir == 1) query = query.OrderBy(orderBy);
else query = query.OrderByDescending(orderBy);
2022-09-06 07:54:48 +03:00
}
2022-09-09 06:44:33 +03:00
2022-09-06 00:42:45 +03:00
return (query.Any(), query.ToArray());
}