added NotContains

This commit is contained in:
Jon Galloway 2019-03-19 14:54:48 -07:00
parent 4398c908c9
commit d97c540ad8
5 changed files with 59 additions and 0 deletions

View File

@ -170,5 +170,29 @@ namespace PoweredSoft.DynamicLinq.Test
var q3b = Persons.AsQueryable().Where(t => t.LastName.Contains("ee") || t.LastName.Contains("ar")); var q3b = Persons.AsQueryable().Where(t => t.LastName.Contains("ee") || t.LastName.Contains("ar"));
QueryableAssert.AreEqual(q3, q3b); QueryableAssert.AreEqual(q3, q3b);
} }
[TestMethod]
public void NotContains()
{
var q1 = Persons.AsQueryable().Query(t => t.NotContains("LastName", "ee"));
var q1b = Persons.AsQueryable().Where(t => !t.LastName.Contains("ee"));
QueryableAssert.AreEqual(q1, q1b);
var q2 = Persons.AsQueryable().Query(t => t.NotContains("LastName", "ee").AndNotContains("FirstName", "vid"));
var q2b = Persons.AsQueryable().Where(t => !t.LastName.Contains("ee") && !t.FirstName.Contains("vid"));
QueryableAssert.AreEqual(q2, q2b);
var q3 = Persons.AsQueryable().Query(t => t.NotContains("LastName", "ee").OrNotContains("LastName", "ar"));
var q3b = Persons.AsQueryable().Where(t => !t.LastName.Contains("ee") || !t.LastName.Contains("ar"));
QueryableAssert.AreEqual(q3, q3b);
}
[TestMethod]
public void ContainsAndNotContains()
{
var q1 = Persons.AsQueryable().Query(t => t.Contains("LastName", "s").AndNotContains("LastName", "r"));
var q1b = Persons.AsQueryable().Where(t => t.LastName.Contains("s") && !t.LastName.Contains("r"));
QueryableAssert.AreEqual(q1, q1b);
}
} }
} }

View File

@ -60,6 +60,22 @@ namespace PoweredSoft.DynamicLinq.Test
QueryableAssert.AreEqual(a, b, "CaseInsensitive"); QueryableAssert.AreEqual(a, b, "CaseInsensitive");
} }
[TestMethod]
public void NotContains()
{
IQueryable<MockPersonObject> a, b;
// case sensitive.
a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "vi", stringComparision: null));
b = Persons.AsQueryable().Where(t => !t.FirstName.Contains("vi"));
QueryableAssert.AreEqual(a, b, "CaseSensitive");
// not case sensitive
a = Persons.AsQueryable().Query(t => t.NotContains("FirstName", "VI", stringComparision: StringComparison.OrdinalIgnoreCase));
b = Persons.AsQueryable().Where(t => t.FirstName.IndexOf("VI", StringComparison.OrdinalIgnoreCase) == -1);
QueryableAssert.AreEqual(a, b, "CaseInsensitive");
}
[TestMethod] [TestMethod]
public void StartsWith() public void StartsWith()
{ {

View File

@ -16,6 +16,7 @@ namespace PoweredSoft.DynamicLinq
LessThan, LessThan,
LessThanOrEqual, LessThanOrEqual,
Contains, Contains,
NotContains,
StartsWith, StartsWith,
EndsWith, EndsWith,
In, In,

View File

@ -97,6 +97,17 @@ namespace PoweredSoft.DynamicLinq.Fluent
=> Or(path, ConditionOperators.Contains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); => Or(path, ConditionOperators.Contains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision);
#endregion #endregion
#region not contains
public WhereBuilder NotContains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null)
=> And(path, ConditionOperators.NotContains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision);
public WhereBuilder AndNotContains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null)
=> And(path, ConditionOperators.NotContains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision);
public WhereBuilder OrNotContains(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null)
=> Or(path, ConditionOperators.NotContains, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision);
#endregion
#region starts with #region starts with
public WhereBuilder StartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null) public WhereBuilder StartsWith(string path, object value, QueryConvertStrategy convertStrategy = QueryConvertStrategy.ConvertConstantToComparedPropertyOrField, QueryCollectionHandling collectionHandling = QueryCollectionHandling.Any, StringComparison? stringComparision = null)
=> And(path, ConditionOperators.StartsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision); => And(path, ConditionOperators.StartsWith, value, convertStrategy: convertStrategy, collectionHandling: collectionHandling, stringComparision: stringComparision);

View File

@ -61,6 +61,13 @@ namespace PoweredSoft.DynamicLinq.Helpers
else else
ret = Expression.Call(member, Constants.ContainsMethod, constant); ret = Expression.Call(member, Constants.ContainsMethod, constant);
} }
else if (conditionOperator == ConditionOperators.NotContains)
{
if (member.Type == stringType && stringComparision.HasValue)
ret = Expression.GreaterThan(Expression.Not(Expression.Call(member, Constants.IndexOfMethod, constant, Expression.Constant(stringComparision.Value))), Expression.Constant(-1));
else
ret = Expression.Not(Expression.Call(member, Constants.ContainsMethod, constant));
}
else if (conditionOperator == ConditionOperators.StartsWith) else if (conditionOperator == ConditionOperators.StartsWith)
{ {
if (member.Type == stringType && stringComparision.HasValue) if (member.Type == stringType && stringComparision.HasValue)