Click here to Skip to main content
15,896,489 members
Articles / General Programming / Architecture

Binding Web Pages with nHydrate

,
Rate me:
Please Sign up or sign in to vote.
4.71/5 (5 votes)
1 Jun 2011Ms-PL12 min read 21K   256   14  
Bind your UI controls to generated objects generically
using System;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace Acme.EFExample.EFDAL
{
	internal class DBHelper
	{
		internal static IDbConnection GetConnection()
		{
			return new SqlConnection(EFExampleEntities.GetConnectionString());
		}

		internal static IDbCommand GetCommand(string commandText, CommandType commandType, IDbConnection connection)
		{
			SqlCommand cmd = new SqlCommand(commandText);
			cmd.CommandType = commandType;
			cmd.Connection = (SqlConnection)connection;
			return cmd;
		}

		internal static void AddParameter(IDbCommand cmd, string parameterName, object value)
		{
			SqlParameter sqlParam = new SqlParameter(parameterName, value);
			cmd.Parameters.Add(sqlParam);
		}

		internal static void AddReturnParameter(IDbCommand cmd)
		{
			SqlParameter sqlParam = new SqlParameter();
			sqlParam.ParameterName = "@RETURN_VALUE";
			sqlParam.Direction = ParameterDirection.ReturnValue;
			cmd.Parameters.Add(sqlParam);
		}

	}

	/// <summary>
	/// An class for extension methods
	/// </summary>
	public static class PublicExtensions
	{
		/// <summary>
		/// Remove all objects in this list and marks them for removal from the database
		/// </summary>
		/// <typeparam name="T">The entity type</typeparam>
		/// <param name="list">The child list of entities to remove</param>
		/// <param name="context">The context in which this list of items exist</param>
		public static void Purge<T>(this System.Data.Objects.DataClasses.EntityCollection<T> list, EFExampleEntities context)
			where T : Widgetsphere.EFCore.DataAccess.NHEntityObject
		{
			var l = new System.Collections.Generic.List<T>(list);
			var c = l.Count;
			for (int ii = 0; ii < c; ii++) context.DeleteItem(l[ii]);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.Country item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.Country, TResult>> selector, TResult newValue)
		{
			SetValue(item, selector, newValue, false);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		/// <param name="fixLength">Determines if the length should be truncated if too long. When false, an error will be raised if data is too large to be assigned to the field.</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.Country item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.Country, TResult>> selector, TResult newValue, bool fixLength)
		{
			var b = selector.Body.ToString();
			var arr = b.Split('.');
			if (arr.Length != 2) throw new System.Exception("Invalid selector");
			var tn = arr.Last();
			var te = (Acme.EFExample.EFDAL.Entity.Country.FieldNameConstants)Enum.Parse(typeof(Acme.EFExample.EFDAL.Entity.Country.FieldNameConstants), tn, true);
			item.SetValue(te, newValue);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.Customer item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.Customer, TResult>> selector, TResult newValue)
		{
			SetValue(item, selector, newValue, false);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		/// <param name="fixLength">Determines if the length should be truncated if too long. When false, an error will be raised if data is too large to be assigned to the field.</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.Customer item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.Customer, TResult>> selector, TResult newValue, bool fixLength)
		{
			var b = selector.Body.ToString();
			var arr = b.Split('.');
			if (arr.Length != 2) throw new System.Exception("Invalid selector");
			var tn = arr.Last();
			var te = (Acme.EFExample.EFDAL.Entity.Customer.FieldNameConstants)Enum.Parse(typeof(Acme.EFExample.EFDAL.Entity.Customer.FieldNameConstants), tn, true);
			item.SetValue(te, newValue);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.Region item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.Region, TResult>> selector, TResult newValue)
		{
			SetValue(item, selector, newValue, false);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		/// <param name="fixLength">Determines if the length should be truncated if too long. When false, an error will be raised if data is too large to be assigned to the field.</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.Region item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.Region, TResult>> selector, TResult newValue, bool fixLength)
		{
			var b = selector.Body.ToString();
			var arr = b.Split('.');
			if (arr.Length != 2) throw new System.Exception("Invalid selector");
			var tn = arr.Last();
			var te = (Acme.EFExample.EFDAL.Entity.Region.FieldNameConstants)Enum.Parse(typeof(Acme.EFExample.EFDAL.Entity.Region.FieldNameConstants), tn, true);
			item.SetValue(te, newValue);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.SystemUser item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.SystemUser, TResult>> selector, TResult newValue)
		{
			SetValue(item, selector, newValue, false);
		}

		/// <summary>
		/// Assigns a value to a field on this object.
		/// </summary>
		/// <param name="item">The entity to set</param>
		/// <param name="selector">The field on the entity to set</param>
		/// <param name="newValue">The new value to assign to the field</param>
		/// <param name="fixLength">Determines if the length should be truncated if too long. When false, an error will be raised if data is too large to be assigned to the field.</param>
		public static void SetValue<TResult>(this Acme.EFExample.EFDAL.Entity.SystemUser item, System.Linq.Expressions.Expression<System.Func<Acme.EFExample.EFDAL.Entity.SystemUser, TResult>> selector, TResult newValue, bool fixLength)
		{
			var b = selector.Body.ToString();
			var arr = b.Split('.');
			if (arr.Length != 2) throw new System.Exception("Invalid selector");
			var tn = arr.Last();
			var te = (Acme.EFExample.EFDAL.Entity.SystemUser.FieldNameConstants)Enum.Parse(typeof(Acme.EFExample.EFDAL.Entity.SystemUser.FieldNameConstants), tn, true);
			item.SetValue(te, newValue);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Country> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Country> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Country, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Country> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Country> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Country, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Country, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(where, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Country> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Country> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Country, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, orderAscending, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Country> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Country> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Country, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Country, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			var index = paging.PageIndex;
			var rpp = paging.RecordsperPage;
			if (index < 1) index = 1;
			if (rpp < 1) rpp = 1;

			paging.RecordCount = item.Count(where);
			paging.PageCount = paging.RecordCount / rpp;
			if ((paging.RecordCount % rpp) != 0) paging.PageCount++;

			var q = item.Where(where);

			if (orderAscending)
				q = q.OrderBy(orderBy);
			else
				q = q.OrderByDescending(orderBy);

			return q.Skip((index - 1) * rpp)
							.Take(rpp)
							.ToList();

		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectQuery from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Customer> GetPagedResults<TKey>(this System.Data.Objects.ObjectQuery<Acme.EFExample.EFDAL.Entity.Customer> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Customer, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectQuery from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Customer> GetPagedResults<TKey>(this System.Data.Objects.ObjectQuery<Acme.EFExample.EFDAL.Entity.Customer> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Customer, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Customer, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(where, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectQuery from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Customer> GetPagedResults<TKey>(this System.Data.Objects.ObjectQuery<Acme.EFExample.EFDAL.Entity.Customer> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Customer, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, orderAscending, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectQuery from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Customer> GetPagedResults<TKey>(this System.Data.Objects.ObjectQuery<Acme.EFExample.EFDAL.Entity.Customer> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Customer, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Customer, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			var index = paging.PageIndex;
			var rpp = paging.RecordsperPage;
			if (index < 1) index = 1;
			if (rpp < 1) rpp = 1;

			paging.RecordCount = item.Count(where);
			paging.PageCount = paging.RecordCount / rpp;
			if ((paging.RecordCount % rpp) != 0) paging.PageCount++;

			var q = item.Where(where);

			if (orderAscending)
				q = q.OrderBy(orderBy);
			else
				q = q.OrderByDescending(orderBy);

			return q.Skip((index - 1) * rpp)
							.Take(rpp)
							.ToList();

		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.CustomerType> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.CustomerType> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.CustomerType, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.CustomerType> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.CustomerType> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.CustomerType, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.CustomerType, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(where, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.CustomerType> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.CustomerType> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.CustomerType, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, orderAscending, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.CustomerType> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.CustomerType> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.CustomerType, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.CustomerType, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			var index = paging.PageIndex;
			var rpp = paging.RecordsperPage;
			if (index < 1) index = 1;
			if (rpp < 1) rpp = 1;

			paging.RecordCount = item.Count(where);
			paging.PageCount = paging.RecordCount / rpp;
			if ((paging.RecordCount % rpp) != 0) paging.PageCount++;

			var q = item.Where(where);

			if (orderAscending)
				q = q.OrderBy(orderBy);
			else
				q = q.OrderByDescending(orderBy);

			return q.Skip((index - 1) * rpp)
							.Take(rpp)
							.ToList();

		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Region> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Region> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Region, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Region> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Region> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Region, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Region, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(where, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Region> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Region> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Region, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, orderAscending, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.Region> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.Region> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Region, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.Region, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			var index = paging.PageIndex;
			var rpp = paging.RecordsperPage;
			if (index < 1) index = 1;
			if (rpp < 1) rpp = 1;

			paging.RecordCount = item.Count(where);
			paging.PageCount = paging.RecordCount / rpp;
			if ((paging.RecordCount % rpp) != 0) paging.PageCount++;

			var q = item.Where(where);

			if (orderAscending)
				q = q.OrderBy(orderBy);
			else
				q = q.OrderByDescending(orderBy);

			return q.Skip((index - 1) * rpp)
							.Take(rpp)
							.ToList();

		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.SystemUser> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.SystemUser> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.SystemUser, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.SystemUser> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.SystemUser> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.SystemUser, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.SystemUser, TKey>> orderBy,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(where, orderBy, true, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.SystemUser> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.SystemUser> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.SystemUser, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			return item.GetPagedResults(x => true, orderBy, orderAscending, paging);
		}

		/// <summary>
		/// Pulls a paged set of data based on the paging criteria
		/// </summary>
		/// <param name="item">The ObjectSet from which to pull data</param>
		/// <param name="where">The filter by which to pull data</param>
		/// <param name="orderBy">The sort order of this data set</param>
		/// <param name="orderAscending">The direction of sort</param>
		/// <param name="paging">The paging object that controls how data is selected. It will contain additional paging information on output.</param>
		public static IEnumerable<Acme.EFExample.EFDAL.Entity.SystemUser> GetPagedResults<TKey>(this System.Data.Objects.ObjectSet<Acme.EFExample.EFDAL.Entity.SystemUser> item,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.SystemUser, bool>> where,
			System.Linq.Expressions.Expression<Func<Acme.EFExample.EFDAL.Entity.SystemUser, TKey>> orderBy,
			bool orderAscending,
			Widgetsphere.EFCore.DataAccess.Paging paging)
		{
			var index = paging.PageIndex;
			var rpp = paging.RecordsperPage;
			if (index < 1) index = 1;
			if (rpp < 1) rpp = 1;

			paging.RecordCount = item.Count(where);
			paging.PageCount = paging.RecordCount / rpp;
			if ((paging.RecordCount % rpp) != 0) paging.PageCount++;

			var q = item.Where(where);

			if (orderAscending)
				q = q.OrderBy(orderBy);
			else
				q = q.OrderByDescending(orderBy);

			return q.Skip((index - 1) * rpp)
							.Take(rpp)
							.ToList();

		}

	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)



Written By
Software Developer (Senior) Hewlett Packard
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions