Click here to Skip to main content
15,886,137 members
Articles / Web Development / ASP.NET

Filtering LINQ Queries Using Business Rules Engine

19 Nov 2012CPOL7 min read 35.4K   779   22  
This article discusses the use of one such new feature, namely, rule-based filtering of LINQ queries using Web Rule, the XML-based super-fast rules engine, implementable as an ASP.NET or MVC component.

This article is in the Product Showcase section for our sponsors at CodeProject. These articles are intended to provide you with information on products and services that we consider useful and of value to developers.

using System.Linq;
using System.Web.Mvc;
using System.Reflection;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Collections.Generic;
using CodeEffects.Rule.Common;


namespace CodeEffects.Rule.Demo.Filter.Mvc.Models
{
	// The utility service that is used to get static data for drop downs
	// and dynamic menu data sources (read more about them here:
	// http://rule.codeeffects.com/Doc/Business-Rules-Dynamic-Menu-Data-Sources)
	public class Utility
	{
		public static List<SelectListItem> GetEntities()
		{
			List<SelectListItem> entities = new List<SelectListItem>();
			entities.Add(new SelectListItem { Text = "Customers", Value = "Customers" });
			entities.Add(new SelectListItem { Text = "Products", Value = "Products" });
			entities.Add(new SelectListItem { Text = "Orders", Value = "Orders" });
			return entities;
		}
		public static List<SelectListItem> GetThemes()
		{
			List<SelectListItem> themes = new List<SelectListItem>();
			themes.Add(new SelectListItem { Text = ThemeType.Gray.ToString(), Value = ThemeType.Gray.ToString() });
			themes.Add(new SelectListItem { Text = ThemeType.Blue.ToString(), Value = ThemeType.Blue.ToString() });
			themes.Add(new SelectListItem { Text = ThemeType.Green.ToString(), Value = ThemeType.Green.ToString() });
			themes.Add(new SelectListItem { Text = ThemeType.Red.ToString(), Value = ThemeType.Red.ToString() });
			themes.Add(new SelectListItem { Text = ThemeType.White.ToString(), Value = ThemeType.White.ToString() });
			themes.Add(new SelectListItem { Text = ThemeType.Navy.ToString(), Value = ThemeType.Navy.ToString() });
			themes.Add(new SelectListItem { Text = ThemeType.Black.ToString(), Value = ThemeType.Black.ToString() });
			return themes;
		}

		public static List<DataSourceItem> GetCountries()
		{
			return GetLookupItems<Country>(new WebRuleLinqEntities().Countries, typeof(Country).GetProperty("Name"));
		}
		public static List<DataSourceItem> GetColors()
		{
			return GetLookupItems<Color>(new WebRuleLinqEntities().Colors, typeof(Color).GetProperty("Name"));
		}
		public static List<DataSourceItem> GetSizes()
		{
			return GetLookupItems<Size>(new WebRuleLinqEntities().Sizes, typeof(Size).GetProperty("Name"));
		}
		public static List<DataSourceItem> GetProductTypes()
		{
			return GetLookupItems<ProductType>(new WebRuleLinqEntities().ProductTypes, typeof(ProductType).GetProperty("Name"));
		}

		private static List<DataSourceItem> GetLookupItems<T> (ObjectSet<T> items, PropertyInfo pi) where T: EntityObject, new()
		{
			List<DataSourceItem> list = new List<DataSourceItem>();

			var q = from c in items.OrderBy("it.Name")
					select c;

			foreach(var item in q)
				list.Add(
					new DataSourceItem(
						(int)item.EntityKey.EntityKeyValues[0].Value,
						pi.GetValue(item, null).ToString()));

			return list;
		}
	}
}

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 Code Project Open License (CPOL)


Written By
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