Line data Source code
1 : using System.Collections.Generic;
2 : using Cqrs.Azure.DocumentDb.DataStores;
3 : using Cqrs.Azure.DocumentDb.Entities;
4 :
5 : namespace System.Linq
6 : {
7 : /// <summary>
8 : /// Provides a set of static (Shared in Visual Basic) methods for querying objects that inherit <see cref="AzureDocumentDbDataStore{TData}"/>.
9 : /// </summary>
10 : public static class AzureDocumentDbDataStoreExtensions
11 1 : {
12 : /// <summary>
13 : /// Returns the only element of the sequence, and throws an exception if there is not exactly one element in the sequence.
14 : /// </summary>
15 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
16 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
17 : /// <returns>The single element of the sequence.</returns>
18 : /// <exception cref="InvalidOperationException">The sequence contains more than one element, or the sequence is empty.</exception>
19 1 : public static TData Single<TData>(this AzureDocumentDbDataStore<TData> source)
20 : where TData : AzureDocumentDbEntity
21 : {
22 : return source.AsEnumerable().ToList().Single();
23 : }
24 :
25 : /// <summary>
26 : /// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
27 : /// </summary>
28 : /// <param name="predicate">A function to test an element for a condition.</param>
29 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
30 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
31 : /// <returns>The single element of the sequence.</returns>
32 : /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
33 : /// <exception cref="InvalidOperationException">The sequence is empty.</exception>
34 1 : public static TData Single<TData>(this AzureDocumentDbDataStore<TData> source, Func<TData, bool> predicate)
35 : where TData : AzureDocumentDbEntity
36 : {
37 : IList<TData> result = source.Where(predicate).AsEnumerable().ToList();
38 : return result.Single();
39 : }
40 :
41 : /// <summary>
42 : /// Returns the only element of the sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.
43 : /// </summary>
44 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
45 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
46 : /// <returns>The single element of the sequence.</returns>
47 1 : public static TData SingleOrDefault<TData>(this AzureDocumentDbDataStore<TData> source)
48 : where TData : AzureDocumentDbEntity
49 : {
50 : return source.AsEnumerable().ToList().SingleOrDefault();
51 : }
52 :
53 : /// <summary>
54 : /// Returns the only element of the sequence that satisfies a specified condition or a default value if no such element exists; this method throws an exception if more than one element satisfies the condition.
55 : /// </summary>
56 : /// <param name="predicate">A function to test an element for a condition.</param>
57 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
58 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
59 : /// <returns>The single element of the sequence.</returns>
60 : /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
61 1 : public static TData SingleOrDefault<TData>(this AzureDocumentDbDataStore<TData> source, Func<TData, bool> predicate)
62 : where TData : AzureDocumentDbEntity
63 : {
64 : IList<TData> result = source.Where(predicate).AsEnumerable().ToList();
65 : return result.SingleOrDefault();
66 : }
67 :
68 : /// <summary>
69 : /// Returns the first element of the sequence.
70 : /// </summary>
71 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
72 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
73 : /// <returns>The first element of the sequence.</returns>
74 : /// <exception cref="InvalidOperationException">The sequence contains more than one element, or the sequence is empty.</exception>
75 1 : public static TData First<TData>(this AzureDocumentDbDataStore<TData> source)
76 : where TData : AzureDocumentDbEntity
77 : {
78 : return source.AsEnumerable().ToList().First();
79 : }
80 :
81 : /// <summary>
82 : /// Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.
83 : /// </summary>
84 : /// <param name="predicate">A function to test an element for a condition.</param>
85 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
86 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
87 : /// <returns>The first element of the sequence.</returns>
88 : /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
89 : /// <exception cref="InvalidOperationException">The sequence is empty.</exception>
90 1 : public static TData First<TData>(this AzureDocumentDbDataStore<TData> source, Func<TData, bool> predicate)
91 : where TData : AzureDocumentDbEntity
92 : {
93 : IList<TData> result = source.Where(predicate).AsEnumerable().ToList();
94 : return result.First();
95 : }
96 :
97 : /// <summary>
98 : /// Returns the first element of the sequence that satisfies a condition or a default value if no such element is found.
99 : /// </summary>
100 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
101 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
102 : /// <returns>The first element of the sequence.</returns>
103 1 : public static TData FirstOrDefault<TData>(this AzureDocumentDbDataStore<TData> source)
104 : where TData : AzureDocumentDbEntity
105 : {
106 : return source.AsEnumerable().ToList().FirstOrDefault();
107 : }
108 :
109 : /// <summary>
110 : /// Returns the first element of a sequence, or a default value if the sequence contains no elements.
111 : /// </summary>
112 : /// <param name="predicate">A function to test an element for a condition.</param>
113 : /// <param name="source">The sequence to type as <see cref="IEnumerable{T}"/>.</param>
114 : /// <typeparam name="TData">The type of the elements of source.</typeparam>
115 : /// <returns>The first element of the sequence.</returns>
116 : /// <exception cref="ArgumentNullException"><paramref name="predicate"/> is null.</exception>
117 1 : public static TData FirstOrDefault<TData>(this AzureDocumentDbDataStore<TData> source, Func<TData, bool> predicate)
118 : where TData : AzureDocumentDbEntity
119 : {
120 : IList<TData> result = source.Where(predicate).AsEnumerable().ToList();
121 : return result.FirstOrDefault();
122 : }
123 : }
124 : }
|