using System;
using System.Linq;
using System.Linq.Expressions;

namespace DataAccess.Repository.Abstract {
	/// <summary>
	/// /// A solution from :
	/// http://www.rosscode.com/blog/index.php?title=the_repository_pattern_andash_iarsquo_m_&more=1&c=1&tb=1&pb=1
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public abstract class QueryBase<T> {
		public abstract Expression<Func<T, bool>> MatchingCriteria {
			get;
		}

		public T SatisfyingElementFrom(IQueryable<T> candidates) {
			return SatisfyingElementsFrom(candidates).Single();
		}

		public IQueryable<T> SatisfyingElementsFrom(IQueryable<T> candidates) {
			return candidates.Where(MatchingCriteria).AsQueryable();
		}
	}
}
