download source code
Materials
- Visual studio
- SQL Server 2005 Express
Ingredients
- System.Linq - In order to support LINQ queries in our code
- System.Data.Linq - In order to support the Linq2SQL datacontext etc
I wanted to put as little effort into creating a linq2sql query as possible because I had to create many.
I also wanted my query to be strong typed so any bugs in my query meant my solution simple would not compile. I wanted my queries to include an orderby clause if so needed.
I came to the conclusion I could use IQueryable and the Predicate Builder for all of this.
Detailed description
I created a criteria class which holds my select expression tree, the field we want to order by and the direction etc.We pass this criteria object to the method which does the actual inquiry on the linq2sql datacontext and returns a list of retrieved items.
public class Criteria<T> where T : class
{
public Expression<Func<T, bool> > where;
public Expression <Func<T, IComparable >> orderbyascending;
public Expression <Func<T, IComparable >> orderbydescending;
public int skip = -1;
public int toplist = -1;
}
{
public Expression<Func<T, bool
public Expression
public Expression
public int toplist = -1;
}
public List<aspnet_User> LoadUsersByCriteria(Criteria<aspnet_User> criteria) { try { using (DbDataContext context = new DbDataContext()) { context.Log = Console.Out; var query = context.GetTable<aspnet_User> () as IQueryable <aspnet_User>; if(criteria.where != null) query = query.Where(criteria.where); if (criteria.skip != -1) query = query.Skip(criteria.skip); if(criteria.toplist != -1) query = query.Take(criteria.toplist); if (criteria.orderbyascending != null || criteria.orderbydescending != null) query = criteria.orderbyascending != null ? query.OrderBy(criteria.orderbyascending) : query.OrderByDescending(criteria.orderbydescending); return query.ToList <aspnet_User>(); } } catch (Exception ex) { throw ex; } }
To create a query and retrieve data I simple create a lamda expression tree with the predicate builder and pass it to the method. I retrieve max 20 users which have been active in the last 5 days. like so:
Criteria<aspnet_User> Usercriteria = new Criteria<aspnet_User>();
Usercriteria.where = PredicateBuilder.True<aspnet_User>();
Usercriteria.where = Usercriteria.where.And(user => user.LastActivityDate > Convert.ToDateTime(DateTime.Today.AddDays(-5).ToString()));
Usercriteria.toplist = 20; //get a list of max 20 users
Usercriteria.orderbyascending = user => user.LastActivityDate.ToString();
List<aspnet_User> users = LoadUsersByCriteria(Usercriteria);
After we created this alias we are going to create a database and some membership tables with help of aspnet_regsql.exe. I use this database and alias in all my examples so it's better that you do this.
create alias
Go to all programs -> Microsoft SQL Server 2005 -> Configuration Tools -> Sql Server Configuration Manager

create database

create membership tables
Run aspnet_regsql.exe at location:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
In the wizard select your newly created database named CodeCookBook

Make sure the website is the startup project. Run the example (Ctrl+F5).Create as many users as you like. Create some test users by making users with a username that starts with the word test.
Go to show users to reassure yourself they are stored in the database and retrieved by the queries.Go play a bit with the queries in UserRepository.
download source code
Criteria<aspnet_User> Usercriteria = new Criteria<aspnet_User>();
Usercriteria.where = PredicateBuilder.True<aspnet_User>();
Usercriteria.where = Usercriteria.where.And(user => user.LastActivityDate > Convert.ToDateTime(DateTime.Today.AddDays(-5).ToString()));
Usercriteria.toplist = 20; //get a list of max 20 users
Usercriteria.orderbyascending = user => user.LastActivityDate.ToString();
List<aspnet_User> users = LoadUsersByCriteria(Usercriteria);
Prepare the database
I want to make sure you need to do as little as possible to get up and running with this example. In the example the datacontext has already been configured to a database, and to make sure you don't have to juggle with connectionstrings I want to create a database alias.After we created this alias we are going to create a database and some membership tables with help of aspnet_regsql.exe. I use this database and alias in all my examples so it's better that you do this.
create alias
Go to all programs -> Microsoft SQL Server 2005 -> Configuration Tools -> Sql Server Configuration Manager

create database

create membership tables
Run aspnet_regsql.exe at location:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regsql.exe
In the wizard select your newly created database named CodeCookBook

Run the example
Open the solution in visual studio.Make sure the website is the startup project. Run the example (Ctrl+F5).Create as many users as you like. Create some test users by making users with a username that starts with the word test.
Go to show users to reassure yourself they are stored in the database and retrieved by the queries.Go play a bit with the queries in UserRepository.
download source code
