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