Hello,
I think this could be adapted to work for my current needs, but it seems that a more flexible approach would be nice. Here is what I have done using the standard API:
public static LB.IPredicateExpression PreferredLifeStage(ILifeStageCriteria criteria)
{
LB.IPredicateExpression retVal = new LB.PredicateExpression();
if (criteria.Married) {retVal.AddWithOr(SmallGroupFields.LifeStageCode == "M");}
if (criteria.Single) { retVal.AddWithOr(SmallGroupFields.LifeStageCode == "S"); }
if (criteria.MarriedAndSingle) { retVal.AddWithOr(SmallGroupFields.LifeStageCode == "B"); }
return retVal;
}
public static LB.IPredicateExpression GroupAge(NewExistingAllGroups selectedGroupAge)
{
LB.IPredicateExpression retVal = new LB.PredicateExpression();
switch (selectedGroupAge)
{
case NewExistingAllGroups.NewGroups:
retVal.Add(SmallGroupFields.DateCreated >= Utility.MiscUtilities.GetCurrentTime().Subtract(new TimeSpan(60, 0, 0, 0)));
break;
case NewExistingAllGroups.ExistingGroups:
retVal.Add(SmallGroupFields.DateCreated <= Utility.MiscUtilities.GetCurrentTime().Subtract(new TimeSpan(60, 0, 0, 0)));
break;
case NewExistingAllGroups.AllGroups:
break;
default:
break;
}
return retVal;
}
using (var lbAdapter = Factory.CreateAdapter())
{
LB.RelationPredicateBucket predBucket = new SD.LLBLGen.Pro.ORMSupportClasses.RelationPredicateBucket();
predBucket.PredicateExpression.Add(GroupAge(criteria.GroupAge));
predBucket.PredicateExpression.AddWithAnd(PreferredLifeStage(criteria.LifeStages));
Using Albhari's PredicateBuilder I think it would have looked something like this:
public static Expression<Func<SmallGroupEntity, bool>> GroupAgeLinq(NewExistingAllGroups selectedGroupAge)
{
var retVal = PredicateBuilder.True<SmallGroupEntity>();
switch (selectedGroupAge)
{
case NewExistingAllGroups.NewGroups:
retVal = retVal.And(sg => sg.DateCreated >= DateTime.Now.Subtract(new TimeSpan(60, 0, 0, 0)));
break;
case NewExistingAllGroups.ExistingGroups:
retVal = retVal.And(sg => sg.DateCreated <= DateTime.Now.Subtract(new TimeSpan(60, 0, 0, 0)));
break;
}
return retVal;
}
public static Expression<Func<SmallGroupEntity, bool>> PreferredLifeStageDD(ILifeStageCriteria lifeStage)
{
var retVal = PredicateBuilder.False<SmallGroupEntity>();
if (lifeStage.Married) { retVal.Or(p => p.LifeStageCode == "M"); }
if (lifeStage.Single) { retVal.Or(p => p.LifeStageCode == "S"); }
if (lifeStage.MarriedAndSingle) retVal.Or(p => p.LifeStageCode == "B");
return retVal;
}
using (var dataAccess = Factory.CreateAdapter())
{
var meta = new LinqMetaData(dataAccess);
var myPred = GroupAgeLinq(criteria.GroupAge);
myPred = myPred.And(PreferredLifeStageLinq());
var q = from g in meta.SmallGroup.Where(myPred) select g;
var retVal = ((LBL.ILLBLGenProQuery)q).Execute<EntityCollection<SmallGroupEntity>>;
}
Is there any way with Linq to create a 'Library' of predicates that individualy could contain a combination of 'AND' and 'OR' operaterers that could then be combined together in any combination of AND's and OR's?
Thanks!
Danny