Skip to content

Commit

Permalink
Simplify LINQ queries
Browse files Browse the repository at this point in the history
  • Loading branch information
tpetchel committed Dec 10, 2021
1 parent c71f534 commit a455eec
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
8 changes: 3 additions & 5 deletions Tailspin.SpaceGame.Web/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TailSpin.SpaceGame.Web.Models;
Expand Down Expand Up @@ -47,7 +46,7 @@ public async Task<IActionResult> Index(
"Trio"
},

GameRegions = new List<string>()
GameRegions = new List<string>()
{
"Milky Way",
"Andromeda",
Expand All @@ -60,10 +59,9 @@ public async Task<IActionResult> Index(
try
{
// Form the query predicate.
// This expression selects all scores that match the provided game
// mode and region (map).
// Select all scores that match the provided game mode and region (map).
// Select the score if the game mode or region is empty.
Expression<Func<Score, bool>> queryPredicate = score =>
Func<Score, bool> queryPredicate = score =>
(string.IsNullOrEmpty(mode) || score.GameMode == mode) &&
(string.IsNullOrEmpty(region) || score.GameRegion == region);

Expand Down
16 changes: 8 additions & 8 deletions Tailspin.SpaceGame.Web/IDocumentDBRepository.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System.Threading.Tasks;
using TailSpin.SpaceGame.Web.Models;

using TailSpin.SpaceGame.Web.Models;

namespace TailSpin.SpaceGame.Web
{
public interface IDocumentDBRepository<T> where T : Model
{
{
/// <summary>
/// Retrieves the item from the store with the given identifier.
/// </summary>
/// <returns>
/// A task that represents the asynchronous operation.
/// The task result contains the retrieved item.
/// </returns>
/// <param name="id">The identifier of the item to retrieve.</param>
/// <param name="id">The identifier of the item to retrieve.</param>
Task<T> GetItemAsync(string id);

/// <summary>
Expand All @@ -31,8 +31,8 @@ public interface IDocumentDBRepository<T> where T : Model
/// <param name="page">The 1-based page of results to return.</param>
/// <param name="pageSize">The number of items on a page.</param>
Task<IEnumerable<T>> GetItemsAsync(
Expression<Func<T, bool>> queryPredicate,
Expression<Func<T, int>> orderDescendingPredicate,
Func<T, bool> queryPredicate,
Func<T, int> orderDescendingPredicate,
int page = 1,
int pageSize = 10
);
Expand All @@ -45,6 +45,6 @@ Task<IEnumerable<T>> GetItemsAsync(
/// The task result contains the number of items that match the query predicate.
/// </returns>
/// <param name="queryPredicate">Predicate that specifies which items to select.</param>
Task<int> CountItemsAsync(Expression<Func<T, bool>> queryPredicate);
Task<int> CountItemsAsync(Func<T, bool> queryPredicate);
}
}
14 changes: 6 additions & 8 deletions Tailspin.SpaceGame.Web/LocalDocumentDBRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Text.Json;
using TailSpin.SpaceGame.Web.Models;
Expand Down Expand Up @@ -46,17 +45,16 @@ public Task<T> GetItemAsync(string id)
/// <param name="page">The 1-based page of results to return.</param>
/// <param name="pageSize">The number of items on a page.</param>
public Task<IEnumerable<T>> GetItemsAsync(
Expression<Func<T, bool>> queryPredicate,
Expression<Func<T, int>> orderDescendingPredicate,
Func<T, bool> queryPredicate,
Func<T, int> orderDescendingPredicate,
int page = 1, int pageSize = 10
)
{
var result = _items.AsQueryable()
var result = _items
.Where(queryPredicate) // filter
.OrderByDescending(orderDescendingPredicate) // sort
.Skip(page * pageSize) // find page
.Take(pageSize) // take items
.AsEnumerable(); // make enumeratable
.Take(pageSize); // take items

return Task<IEnumerable<T>>.FromResult(result);
}
Expand All @@ -69,9 +67,9 @@ public Task<IEnumerable<T>> GetItemsAsync(
/// The task result contains the number of items that match the query predicate.
/// </returns>
/// <param name="queryPredicate">Predicate that specifies which items to select.</param>
public Task<int> CountItemsAsync(Expression<Func<T, bool>> queryPredicate)
public Task<int> CountItemsAsync(Func<T, bool> queryPredicate)
{
var count = _items.AsQueryable()
var count = _items
.Where(queryPredicate) // filter
.Count(); // count

Expand Down

0 comments on commit a455eec

Please sign in to comment.