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;
10 : using System.Collections.Generic;
11 : using System.Linq;
12 : using System.Linq.Expressions;
13 : using Cqrs.DataStores;
14 :
15 : namespace Cqrs.Sql.DataStores
16 : {
17 : /// <summary>
18 : /// A collection of extension methods for <see cref="IDataStore{TData}"/>
19 : /// </summary>
20 : public static class SqlDataStoreExtensions
21 1 : {
22 : /// <summary>
23 : /// Use this one... Filters a sequence of values based on a predicate.
24 : /// </summary>
25 1 : public static IQueryable<TEntity> Where<TEntity, TDbEntity>(this IDataStore<TEntity> dataStore, Expression<Func<TEntity, bool>> predicate)
26 : where TDbEntity : class, new()
27 : {
28 : var sqlDataStore = dataStore as SqlDataStore<TEntity, TDbEntity>;
29 : if (sqlDataStore != null)
30 : {
31 : Expression expression = sqlDataStore.ExpressionConverter.Visit(predicate);
32 :
33 : return new SqlDataStore<TEntity, TDbEntity>
34 : (
35 : sqlDataStore.DataContext,
36 : sqlDataStore.DataTable,
37 : sqlDataStore.DbEntityQuery.Where((Expression<Func<TDbEntity, bool>>)expression),
38 : sqlDataStore.EntityQuery.Where(predicate)
39 : );
40 : }
41 :
42 : return dataStore.Where(predicate);
43 : }
44 :
45 : /// <summary>
46 : /// Use this one... Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.
47 : /// </summary>
48 1 : public static TEntity Single<TEntity, TDbEntity>(this IEnumerable<TEntity> dataStore)
49 : where TDbEntity : class, new()
50 : where TEntity : new()
51 : {
52 : var sqlDataStore = dataStore as SqlDataStore<TEntity, TDbEntity>;
53 : if (sqlDataStore != null)
54 : {
55 : TDbEntity result = sqlDataStore.DbEntityQuery.Single();
56 : var convertedResult = Converters.ConvertTo<TEntity>(result);
57 : return convertedResult;
58 : }
59 : return dataStore.Single();
60 : }
61 : }
62 : }
|