Skip to content

Instantly share code, notes, and snippets.

@MikeLarned
Created February 5, 2012 19:10
Show Gist options
  • Save MikeLarned/1747390 to your computer and use it in GitHub Desktop.
Save MikeLarned/1747390 to your computer and use it in GitHub Desktop.
Search IQuery Example 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using NHibernate;
using NHibernate.Linq;
namespace Test.Core.Domain
{
public class MyQuery : IQuery<IEnumerable<Scam>>
{
private readonly string _scamType;
private readonly DateTime? _start;
private readonly DateTime? _end;
private readonly string _gender;
public MyQuery (string scamType = "", DateTime? start = null, DateTime? end = null, string gender = "")
{
_scamType = scamType;
_start = start;
_end = end;
_gender = gender;
}
public IEnumerable<Scam> Execute(ISession session)
{
var query = session.Query<Scam>()
.Where(GetScamTypeFilter())
.Where(GetDateRangeFilter())
.Where(GetGenderFilter());
return query.ToList();
}
private Expression<Func<Scam, bool>> GetScamTypeFilter()
{
if (!String.IsNullOrEmpty(_scamType))
return x => x.ScamType == _scamType;
return x => true;
}
private Expression<Func<Scam, bool>> GetDateRangeFilter()
{
if (_start != null && _end != null)
return x => x.ScameDate >= _start && x.ScameDate <= _end;
return x => true;
}
private Expression<Func<Scam, bool>> GetGenderFilter()
{
if (!String.IsNullOrEmpty(_gender))
return x => x.Gender == _gender;
return x => true;
}
}
public class Scam
{
public int Id { get; set; }
public string ScamType { get; set; }
public DateTime ScameDate { get; set; }
public string Gender { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment