I have a LINQ statement I want to convert it into Expression Tree
public class tblEmpLocation
{
public uint EmpLocationId { get; set; }
public uint? EmpId { get; set; }
public uint? LocationId { get; set; }
public DateTime EffectiveDt { get; set; } = DateTime.Now;
}
We have employee location class basically have the location the Employee Id and Location Id.
public class temptable
{
public uint Id{ get; set; }
public DateTime EffectiveDt { get; set; }
}
We have the temp class which basically contain the Id and Effective date that is a sample class. Now we have similar table like employee Department, employee salary etc. so we want to create an linq extension that basically take the class as input and get the desired result
List<tblEmpLocation> tbls = new List<tblEmpLocation>();
var data=tbls.GroupBy(p => p.EmpId).Select(q => new temptable{ Id=q.Key, EffectiveDt=q.Max(r => r.EffectiveDt) });
thanks for help
This is universal implementation of DistinctBy method, which returns last record of the group.
Schematically when you make the following call:
query = query.DistinctBy(e => e.EmpId, e => e.EffectiveDt);
// or with complex keys
query = query.DistinctBy(e => new { e.EmpId, e.Other }, e => new { e.EffectiveDt, e.SomeOther});
Or fully dynamic
query = query.DistinctBy("EmpId", "EffectiveDt");
Function generates the following query:
query =
from d in query.Select(d => d.EmpId).Distinct()
from e in query
.Where(e => e.EmpId == d)
.OrderByDescending(e => e.EffectiveDt)
.Take(1)
select e;
Or with complex keys:
query =
from d in query.Select(d => new { d.EmpId, d.Other }).Distinct()
from e in query
.Where(e => e.EmpId == d.EmpId && e.Other == d.Other)
.OrderByDescending(e => e.EffectiveDt)
.ThenByDescending(e => e.SomeOther)
.Take(1)
select e;
And realisation:
public static class QueryableExtensions
{
public static IQueryable<T> DistinctBy<T>(
this IQueryable<T> source,
string distinctPropName,
string maxPropName)
{
var entityParam = Expression.Parameter(typeof(T), "e");
var distinctBy = Expression.Lambda(MakePropPath(entityParam, distinctPropName), entityParam);
var maxBy = Expression.Lambda(MakePropPath(entityParam, maxPropName), entityParam);
var queryExpression = Expression.Call(typeof(QueryableExtensions), nameof(QueryableExtensions.DistinctBy),
new[] { typeof(T), distinctBy.Body.Type, maxBy.Body.Type },
Expression.Constant(source),
Expression.Quote(distinctBy),
Expression.Quote(maxBy));
var executionLambda = Expression.Lambda<Func<IQueryable<T>>>(queryExpression);
return executionLambda.Compile()();
}
public static IQueryable<T> DistinctBy<T, TKey, TMax>(
this IQueryable<T> source,
Expression<Func<T, TKey>> distinctBy,
Expression<Func<T, TMax>> maxBy)
{
var distinctQuery = source.Select(distinctBy).Distinct();
var distinctParam = Expression.Parameter(typeof(TKey), "d");
var entityParam = distinctBy.Parameters[0];
var mapping = MapMembers(distinctBy.Body, distinctParam).ToList();
var orderParam = maxBy.Parameters[0];
var oderMapping = CollectMembers(maxBy.Body).ToList();
var whereExpr = mapping.Select(t => Expression.Equal(t.Item1, t.Item2))
.Aggregate(Expression.AndAlso);
var whereLambda = Expression.Lambda(whereExpr, entityParam);
// d => query.Where(x => d.distinctBy == x.distinctBy).Take(1)
Expression selectExpression = Expression.Call(typeof(Queryable), nameof(Queryable.Where), new[] { typeof(T) },
source.Expression,
whereLambda);
// prepare OrderByPart
for (int i = 0; i < oderMapping.Count; i++)
{
var orderMethod = i == 0 ? nameof(Queryable.OrderByDescending) : nameof(Queryable.ThenByDescending);
var orderItem = oderMapping[i];
selectExpression = Expression.Call(typeof(Queryable), orderMethod, new[] { typeof(T), orderItem.Type },
selectExpression, Expression.Lambda(orderItem, orderParam));
}
// Take(1)
selectExpression = Expression.Call(typeof(Queryable), nameof(Queryable.Take), new[] { typeof(T) },
selectExpression,
Expression.Constant(1));
var selectManySelector =
Expression.Lambda<Func<TKey, IEnumerable<T>>>(selectExpression, distinctParam);
var selectManyQuery = Expression.Call(typeof(Queryable), nameof(Queryable.SelectMany),
new[] { typeof(TKey), typeof(T) }, distinctQuery.Expression, selectManySelector);
return source.Provider.CreateQuery<T>(selectManyQuery);
}
static Expression MakePropPath(Expression objExpression, string path)
{
return path.Split('.').Aggregate(objExpression, Expression.PropertyOrField);
}
private static IEnumerable<Tuple<Expression, Expression>> MapMembers(Expression expr, Expression projectionPath)
{
switch (expr.NodeType)
{
case ExpressionType.New:
{
var ne = (NewExpression)expr;
for (int i = 0; i < ne.Arguments.Count; i++)
{
foreach (var e in MapMembers(ne.Arguments[i], Expression.MakeMemberAccess(projectionPath, ne.Members[i])))
{
yield return e;
}
}
break;
}
default:
yield return Tuple.Create(projectionPath, expr);
break;
}
}
private static IEnumerable<Expression> CollectMembers(Expression expr)
{
switch (expr.NodeType)
{
case ExpressionType.New:
{
var ne = (NewExpression)expr;
for (int i = 0; i < ne.Arguments.Count; i++)
{
yield return ne.Arguments[i];
}
break;
}
default:
yield return expr;
break;
}
}
}
Related
i am trying to use projection map to map the property values from source to destination as mentioned here in this question Projection mapping looks like as below but getting error near select statement
Error is : The type arguments for the method
'Enumerable.Select<TSource, Tresult>(IEnumerable,
Func<Tsource, int, Tresult>)' cannot be inferred from usage.Try
specifying type arguments explicitly
and below is the code sample
public static IQueryable<TDest> ProjectionMap<TSource, TDest>(IQueryable<TSource> sourceModel)
where TDest : new()
{
var sourceProperties = typeof(TSource).GetProperties().Where(p => p.CanRead);
var destProperties = typeof(TDest).GetProperties().Where(p => p.CanWrite);
var propertyMap = from d in destProperties
join s in sourceProperties on new { d.Name, d.PropertyType } equals new { s.Name, s.PropertyType }
select new { Source = s, Dest = d };
var itemParam = Expression.Parameter(typeof(TSource), "item");
var memberBindings = propertyMap.Select(p => (MemberBinding)Expression.Bind(p.Dest, Expression.Property(itemParam, p.Source)));
var newExpression = Expression.New(typeof(TDest));
var memberInitExpression = Expression.MemberInit(newExpression, memberBindings);
var projection = Expression.Lambda<Func<TSource, TDest>>(memberInitExpression, itemParam);
return sourceModel.Select(projection);
}
and then i am using above method below
private static MechanicalData TransformMechanicalData(MechanicalData sourceMechanicalData, Dictionary<string, MasterSection> masterSectionMappedLibrary)
{
return new MechanicalData()
{
Acoustic = sourceMechanicalData.Acoustic
.Where(a => a != null)
.Select(ProjectionMap<LibraryAcoustic, LibraryAcoustic>(sourceMechanicalData.Acoustic.AsQueryable())).ToList() ?? new(),
}
}
Could any one please let me know where I am doing wrong, many thanks in advance.
Update:
Acoustic = sourceMechanicalData.Acoustic
.Where(a => a != null)
.Select(acoustic => new LibraryAcoustic
{
Id = acoustic.Id,
IsApproved = true,
NoiseCriteria = acoustic.NoiseCriteria,
SourceOfData = acoustic.SourceOfData,
SourceOfDataId = acoustic.SourceOfData.Id,
MasterSection = masterSectionMappedLibrary["Library Acoustic"]
}).ToList() ?? new(),
calling that transformMechanicalData method in below
if (spaceTypeReader.HasRows)
{
while (spaceTypeReader.Read())
{
var id = spaceTypeReader.IsDBNull(0) ? default : Guid.Parse(spaceTypeReader.GetString(0));
var mechanicalDataJson = spaceTypeReader.IsDBNull(1) ? "null" : spaceTypeReader.GetString(1);
var srcMechanicalDataJson = JsonConvert.DeserializeObject<MechanicalData>(mechanicalDataJson);
fixedSpaceTypesMechanicalData[id] = TransformMechanicalData(srcMechanicalDataJson, masterSectionMappedLibrary);
}
}
and mechanical data class be like
public class MechanicalData
{
public List<LibraryAcoustic> Acoustic { get; set; }
.........
}
Update 2:
model for libraryAcoustic
public class LibraryAcoustic
{
public double? NoiseCriteria { get; set; }
[ForeignKey("SourceOfData")]
public Guid? SourceOfDataId { get; set; }
public virtual CodeStandardGuideline SourceOfData { get; set; }
public Guid Id { get; set; }
public MasterSection MasterSection { get; set; }
public bool? IsApproved { get; set; }
}
FROM Model
"Acoustic": [
{
"Id": "d9254132-d11d-48dd-9b74-c0b60d1e4b8a",
"IsApproved": null,
"SourceOfData": {
"Id": "c5bf3585-50b1-4894-8fad-0ac884343935",
"CodeStandardGuidelineType": "GUIDELINE_OR_STANDARD"
},
"MasterSection": null,
"SourceOfDataId": null,
"NoiseCriteria": 1,
}
],
TO model:
"Acoustic": [
{
"Id": "d9254132-d11d-48dd-9b74-c0b60d1e4b8a",
"IsApproved": true,
"SourceOfData": {
"Id": "c5bf3585-50b1-4894-8fad-0ac88434393",
"CodeStandardGuidelineType": "GUIDELINE_OR_STANDARD"
},
"MasterSection": {Name:"test"},
"SourceOfDataId": "c5bf3585-50b1-4894-8fad-0ac884343935",
"NoiseCriteria": 1,
}
],
Test Class update:
SourceOfData sourceOfData = new SourceOfData()
{
Id = new Guid("c5bf3585-50b1-4894-8fad-0ac884343935"),
Name = "test"
};
TestClassA170 testClassA170 = new TestClassA170()
{
Category = "test",
SourceOfData = sourceOfData,
SourceOfDataId = null,
IsApproved = true,
MinOutdoorAirACH = 1,
MinTotalAirACH = 2,
DirectExhaust = DirectExhaust.NO,
PressureRelationship = PressureRelationship.NEGATIVE,
RecirculatedAir = RecirculatedAir.NO,
SpaceFunction = "10"
};
List<TestClassA170> list = new List<TestClassA170>();
list.Add(testClassA170);
You have to create mapping helper class which accespts additionally Dictionary as paraneter:
public static class PropertyMapper<TSource, TDest>
{
private static Expression<Func<TSource Dictionary<string, MasterSection>, TDest>> _mappingExpression;
private static Func<TSource, Dictionary<string, MasterSection>, TDest> _mapper;
static PropertyMapper()
{
_mappingExpression = ProjectionMap();
_mapper = _mappingExpression.Compile();
}
public static Func<TSource, Dictionary<string, MasterSection>, TDest> Mapper => _mapper;
public static string MasterKeyFromClassName(string className)
{
// you have to do that yourself
throw new NotImplementedException();
}
public static Expression<Func<TSource, Dictionary<string, MasterSection>, TDest>> ProjectionMap()
{
var sourceProperties = typeof(TSource).GetProperties().Where(p => p.CanRead);
var destProperties = typeof(TDest).GetProperties().Where(p => p.CanWrite);
var propertyMap =
from d in destProperties
join s in sourceProperties on new { d.Name, d.PropertyType } equals new { s.Name, s.PropertyType }
where d.Name != "MasterSection"
select new { Source = s, Dest = d };
var itemParam = Expression.Parameter(typeof(TSource), "item");
var dictParam = Expression.Parameter(typeof(Dictionary<string, MasterSection>), "dict");
var memberBindings = propertyMap.Select(p => (MemberBinding)Expression.Bind(p.Dest, Expression.Property(itemParam, p.Source))).ToList();
var masterSectionProp = destProperties.FirstOrDefault(s => s.Name == "MasterSection");
if (masterSectionProp != null)
{
Expression<Func<Dictionary<string, MasterSection>, string, MasterSection>> dictRetrievalTemplate = (dict, value) => dict[value];
var masterPropertyBind = Expression.Bind(masterSectionProp, ExpressionReplacer.GetBody(dictRetrievalTemplate, dictParam, Expression.Constant(MasterKeyFromClassName(typeof(TSource).Name)));
memberBindings.Add(masterPropertyBind);
}
var sourceOfDataProp = destProperties.FirstOrDefault(s => s.Name == "SourceOfDataId");
if (sourceOfDataProp != null)
{
memberBindings.Add(Expression.Bind(sourceOfDataProp, Expression.Property(Expression.Property(itemParam, "SourceOfData"), "Id")));
}
var isApprovedProp = destProperties.FirstOrDefault(s => s.Name == "IsApproved");
if (isApprovedProp != null)
{
memberBindings.Add(Expression.Bind(isApprovedProp, Expression.Constant(true)));
}
var newExpression = Expression.New(typeof(TDest));
var memberInitExpression = Expression.MemberInit(newExpression, memberBindings);
var projection = Expression.Lambda<Func<TSource, Dictionary<string, MasterSection>, TDest>>(memberInitExpression, itemParam, dictParam);
return projection;
}
class ExpressionReplacer : ExpressionVisitor
{
readonly IDictionary<Expression, Expression> _replaceMap;
public ExpressionReplacer(IDictionary<Expression, Expression> replaceMap)
{
_replaceMap = replaceMap ?? throw new ArgumentNullException(nameof(replaceMap));
}
public override Expression Visit(Expression exp)
{
if (exp != null && _replaceMap.TryGetValue(exp, out var replacement))
return replacement;
return base.Visit(exp);
}
public static Expression Replace(Expression expr, Expression toReplace, Expression toExpr)
{
return new ExpressionReplacer(new Dictionary<Expression, Expression> { { toReplace, toExpr } }).Visit(expr);
}
public static Expression Replace(Expression expr, IDictionary<Expression, Expression> replaceMap)
{
return new ExpressionReplacer(replaceMap).Visit(expr);
}
public static Expression GetBody(LambdaExpression lambda, params Expression[] toReplace)
{
if (lambda.Parameters.Count != toReplace.Length)
throw new InvalidOperationException();
return new ExpressionReplacer(Enumerable.Range(0, lambda.Parameters.Count)
.ToDictionary(i => (Expression) lambda.Parameters[i], i => toReplace[i])).Visit(lambda.Body);
}
}
}
Then rewrite your function:
private static Expression<MechanicalData TransformMechanicalData(MechanicalData sourceMechanicalData, Dictionary<string, MasterSection> masterSectionMappedLibrary)
{
return new MechanicalData()
{
Acoustic = sourceMechanicalData.Acoustic
.Where(a => a != null)
.Select(a => PropertyMapper<LibraryAcoustic, LibraryAcoustic>.Mapper(a, masterSectionMappedLibrary)).ToList(),
}
}
I have an Expression Tree to create a dynamic where clause based on the criteria a user selects on a checkbox.
Eg: - User wants to search for: "test"
User selects
1. Prop1
2. Prop2
for an Object
MyDBObject
The search query will look like
dbRecords.Where(r=> r.Prop1.Contains("test") || r.Prop2.Contains("test"))
The reason to use an Expression Tree is so that it can be used for any unknown number of properties of an unknown object.
I almost have it working, but I get Argument Expression is not valid
Also how does one initialize an empty boolean expression other than using
"something that evaluates to -- true/false" ?
I've only read about them for a few hours by now so maybe there's something I didn't see yet.
public static Expression<Func<T, bool>> CreatePredicateFromCrtieriaAndSearchTerm<T>(List<string> checkedCriteria, string searchTerm)
{
// sample checked records
checkedCriteria = new[]
{
new { Name = "Prop1", DisplayValue = "Checkbox value 1" },
new { Name = "Prop2", DisplayValue = "Checkbox value 2" }
}
.Select(x => x.Name).ToList();
var param = Expression.Parameter(typeof(T), "record");
Expression oneEqualsOne = Expression.Equal(Expression.Constant(1), Expression.Constant(1));
// Creates (record => (1=1) AND ...)
Expression<Func<T, bool>> finalExpression = Expression.Lambda<Func<T, bool>>(oneEqualsOne, param);
Console.WriteLine(finalExpression);
try
{
// Iterate through properties, find selected props and create
// (record.SelectedProp1.Contains("searchTerm") || record.SelectedProp2.Contains("searchTerm") ... )
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
List<Expression> matchExpressions = new List<Expression>();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
for (int j = 0; j < checkedCriteria.Count; j++)
{
if (prop.Name == checkedCriteria[j])
{
// add to where expression
Expression left = Expression.Property(param, prop.Name);
MethodInfo contains = typeof(string).GetMethod("Contains", new[] { typeof(string) });
Expression right = Expression.Constant(searchTerm, searchTerm.GetType());
Expression matchExpression = Expression.Call(left, contains, right);
matchExpressions.Add(matchExpression);
}
}
}
// Creates (1=0 OR ... OR ...)
Expression currentPredicateBody = Expression.Equal(Expression.Constant(1), Expression.Constant(0));
foreach (var matchExpression in matchExpressions)
{
currentPredicateBody = Expression.MakeBinary(ExpressionType.OrElse, matchExpression, currentPredicateBody);
Console.WriteLine(currentPredicateBody);
}
// ( (1=0) || record.SelectedProp1.Contains("searchTerm") || record.SelectedProp2.Contains("searchTerm") )
if (matchExpressions.Count > 0)
{
oneEqualsOne = Expression.AndAlso(oneEqualsOne, currentPredicateBody);
Console.WriteLine(oneEqualsOne);
}
// Full expression:
// ( record => (1=1) AND ( (1=0) || record.SelectedProp1.Contains("searchTerm") || record.SelectedProp2.Contains("searchTerm") ))
finalExpression = Expression.Lambda<Func<T, bool>>(oneEqualsOne, new ParameterExpression[] { param });
Console.WriteLine(finalExpression);
}
catch (Exception ex)
{
throw new Exception(string.Format(#"Error occurred creating where predicate from checked criteria: {0}", ex.Message));
}
return finalExpression;
}
internal class MyDBObject
{
public int Id { get; set; }
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public string Prop4 { get; set; }
public string Prop5 { get; set; }
public string Prop11 { get; set; }
public string Prop12 { get; set; }
public string Prop13 { get; set; }
public string Prop14 { get; set; }
public string Prop15 { get; set; }
public string Prop21 { get; set; }
public string Prop22 { get; set; }
public string Prop23 { get; set; }
public string Prop24 { get; set; }
public string Prop25 { get; set; }
}
public static void Main(string[] args)
{
List<MyDBObject> dbRecords = new List<MyDBObject>
{
new MyDBObject { Id = 1, Prop2 = "O1_P2", Prop3 = "O1_P3", Prop12 = "O1_P12", Prop15 = "O1_P15", Prop24 = "O1_P24", Prop25 = "O1_P25" },
new MyDBObject { Id = 2, Prop15 = "O2_P15", Prop21 = "test", Prop22 = "O2_P22", Prop23 = "O2_P23", Prop24 = "O2_P24", Prop25 = "O2_P25" },
new MyDBObject { Id = 3, Prop21 = "O3_P21", Prop22 = "O3_P22", Prop23 = "O3_P23", Prop24 = "test", Prop25 = "O3_P25" }
};
try
{
var predicate = CreatePredicateFromCrtieriaAndSearchTerm<MyDBObject>(null, "test");
var query = dbRecords.AsQueryable().Provider.CreateQuery<MyObject>(predicate);
List<MyObject> results = query.ToList();
foreach (var rs in results)
{
Console.WriteLine("Id: " + rs.Id);
}
}
catch (Exception ex)
{
Console.WriteLine("Error->> " + ex.Message);
}
}
Try this code:
public static Expression<Func<T, bool>> CreatePredicate<T>(List<string> propsToSearch,
string valueToSearch)
{
var parameter = Expression.Parameter(typeof(T), "record");
// filtering is not required
if (!propsToSearch.Any() || string.IsNullOrEmpty(valueToSearch))
return Expression.Lambda<Func<T, bool>>(Expression.Constant(true), parameter);
var props = typeof(T).GetProperties()
.Select(p => p.Name)
.Intersect(propsToSearch.Distinct());
var containsMethod = typeof(string).GetMethod("Contains");
var body = props
.Select(p => Expression.PropertyOrField(parameter, p))
.Aggregate((Expression) Expression.Constant(false),
(c, n) => Expression.OrElse(c,
Expression.Call(n, containsMethod, Expression.Constant(valueToSearch)))
);
var lambda = Expression.Lambda<Func<T, bool>>(body, parameter);
return lambda;
}
It return record => true if there is no properties to search or search patern is empty. QueryProvider can be smart enough to not generate sql where in this case.
Update: I created a demo (it's not working because of security restriction of dotNetFiddle, but localy works fine)
when i use from OrderBy i get this error DbSortClause expressions must have a type that is order comparable. Parameter name: key.
i do not know how to change this code
var resAsc = this.Context.Set<TEntity>().AsNoTracking().Where(predicate: predicate).OrderBy(s => sortItem.SortItems.Select(w => w.SortText).ToList()).Skip(page * size).Take(page).ToList().AsQueryable();
how can i resolve it ?
Edit : I want send parameters to this method for example
string test = "Id";
SortOption objsort = new SortOption();
objsort.SortItems = new List<SortItem>();
objsort.SortItems.Add(new SortItem { SortText = "Id" });
objsort.SortOrderType = EnumTypes.SortOrder.Ascending;
var res = ApplicationService.SearchPage(w => w.Id > 2, objsort, 1, 3);
and now i get these parameters here
public Paginated<TEntity> SearchPage(Expression<Func<TEntity, bool>> predicate, SortOption sortItem, int page, int size)
{
Paginated<TEntity> objPage = new Paginated<TEntity>();
if (sortItem.SortOrderType == EnumTypes.SortOrder.Ascending)
{
var resAsc = this.Context.Set<TEntity>().AsNoTracking().Where(predicate: predicate).OrderBy(s => sortItem.SortItems.Select(w => w.SortText).ToList()).Skip(page * size).Take(page).ToList().AsQueryable();
objPage.Data = resAsc;
objPage.TotalCount = this.Context.Set<TEntity>().Count();
return objPage;
}
var resDesc = this.Context.Set<TEntity>().AsNoTracking().Where(predicate: predicate).OrderByDescending(s => sortItem.SortItems.Select(w => w.SortText)).Skip(page * size).Take(page).ToList().AsQueryable();
objPage.Data = resDesc;
objPage.TotalCount = this.Context.Set<TEntity>().Count();
return objPage;
}
Actually, i want get this Id in the here
var resAsc = this.Context.Set<TEntity>().AsNoTracking().Where(predicate: predicate).OrderBy(s => s.Id).Skip(page * size).Take(page).ToList().AsQueryable();
You could use the following code or take inspiration from it. It gives you an extension method IEnumerable<TEntity> called Prepare. This method will select the items which match an predicate then it will order the entities and finnaly paginate it.
You can give as many ColumnOrderConfiguration objects as you want. It will use OrderBy and ThenBy to create the correct result.
Just keep in mind that you will have to use Expression<Func<,>> instead of Func<,> and IDbSet instead of IEnumerable when you work with an database.
public class ColumnOrderConfiguration<TEntity>
{
public Func<TEntity, object> ValueSelector { get; set; } = entity => null;
public SortOrder SortOrder { get; set; } = SortOrder.Ascending;
}
public static class CollectionPreparationExtensions
{
public static IEnumerable<TEntity> Prepare<TEntity>(this IEnumerable<TEntity> entities, Func<TEntity, bool> predicate, IEnumerable<ColumnOrderConfiguration<TEntity>> orderConfiguration, int pageIndex, int pageSize)
=> entities.Where(predicate).OrderBy(orderConfiguration).Skip(pageIndex * pageSize).Take(pageSize);
private static IEnumerable<TEntity> OrderBy<TEntity>(this IEnumerable<TEntity> entities, IEnumerable<ColumnOrderConfiguration<TEntity>> orderConfiguration)
{
var configurations = orderConfiguration.ToArray();
if (!configurations.Any())
return entities;
var firstOrderConfiguration = configurations.First();
var orderedEntities = entities.OrderBy(firstOrderConfiguration.ValueSelector, firstOrderConfiguration.SortOrder);
for (var i = 1; i < configurations.Length; i++)
{
orderedEntities = orderedEntities.ThenBy(configurations[i].ValueSelector, configurations[i].SortOrder);
}
return orderedEntities;
}
private static IOrderedEnumerable<TEntity> ThenBy<TEntity>(this IOrderedEnumerable<TEntity> entities, Func<TEntity, object> valueSelector, SortOrder sortOrder)
{
if (sortOrder == SortOrder.Descending)
return entities.ThenByDescending(valueSelector);
return entities.ThenBy(valueSelector);
}
private static IOrderedEnumerable<TEntity> OrderBy<TEntity>(this IEnumerable<TEntity> entities, Func<TEntity, object> valueSelector, SortOrder sortOrder)
{
if (sortOrder == SortOrder.Descending)
return entities.OrderByDescending(valueSelector);
return entities.OrderBy(valueSelector);
}
}
And this is how you use it.
public class MyTestEntity
{
public bool IsTrue { get; set; }
public string OrderText { get; set; }
public int ThenOrderBy { get; set; }
}
var entities = new List<MyTestEntity>(new []
{
new MyTestEntity { IsTrue = true, OrderText = "1234", ThenOrderBy = 4321 },
new MyTestEntity { IsTrue = true, OrderText = "000001", ThenOrderBy = 000001 },
new MyTestEntity { IsTrue = false }
});
var searchPredicate = new Func<MyTestEntity, bool>(entity => entity.IsTrue);
var orderConfig = new List<ColumnOrderConfiguration<MyTestEntity>>(new []
{
// first order by `OrderText` ascending
new ColumnOrderConfiguration<MyTestEntity>
{
ValueSelector = entity => entity.OrderText,
SortOrder = SortOrder.Ascending
},
// then order by `ThenOrderBy` descending
new ColumnOrderConfiguration<MyTestEntity>
{
ValueSelector = entity => entity.ThenOrderBy,
SortOrder = SortOrder.Descending
}
});
var pageIndex = 0;
var pageSize = 20;
var result = entities.Prepare(searchPredicate, orderConfig, pageIndex, pageSize);
*****Scroll down for final working solution*****
All of my Entity Framework models use partials, which implement my own IEntity interface:
public interface IEntity
{
int Status { get; set; }
int ID { get; set; }
}
This allows me to filter any Entity which implements this interface, based on the following function (simplified version):
public static IQueryable<T> FilterByStatus<T>(this IQueryable<T> query, int status) where T : class, IEntity
{
return query.Where(m => m.Status == status);
}
Now I want a function which names all of the properties, which I might want to perform a text query on. Let's say that implementation Foo of IEntity has 2 values (Bar and Baz) that I want to perform queries on.
I currently have:
public static IQueryable<Foo> FooSearch(this Entities context, string query)
{
IQueryable<Foo> result = context.Foo;
if (!String.IsNullOrEmpty(query))
{
result = result.Where(m =>
m.Bar.ToLower().IndexOf(query.ToLower()) >= 0 ||
m.Baz.ToLower().IndexOf(query.ToLower()) >= 0);
}
return result;
}
But I want to set it up in a more generic way. Something like:
public interface IEntity
{
int Status { get; set; }
int ID { get; set; }
string[] QueryableProperties { get; set; }
}
And some kind of implementation like (pseudocode):
public static IQueryable<T> GenericSearch(this IQueryable<T> query, string query) where T : class, IEntity
{
if (!String.IsNullOrEmpty(query))
{
query = query.Where(m =>
m[QueryableProperties[0]].ToLower().IndexOf(query.ToLower()) >= 0 ||
m[QueryableProperties[1]].ToLower().IndexOf(query.ToLower()) >= 0 ||
// .... //
m[QueryableProperties[QueryableProperties.Count - 1]].ToLower().IndexOf(query.ToLower()) >= 0)
}
return query;
}
How can I achieve this?
******Working Solution******
Search function:
public static class SearchFilter
{
private static Expression GetNestedPropertyExpression(Expression expression, string propertyName)
{
Expression body = expression;
foreach (var member in propertyName.Split('.'))
{
body = Expression.PropertyOrField(body, member);
}
return body;
}
private static Expression<Func<T, bool>> GetSearchExpression<T>(string[] propertyNames, string query)
{
var parameterExp = Expression.Parameter(typeof(T), "category");
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
MethodInfo toLowerMethod = typeof(string).GetMethod("ToLower", Type.EmptyTypes);
List<Expression> methodCalls = new List<Expression>();
foreach (string propertyName in propertyNames)
{
var propertyExp = GetNestedPropertyExpression(parameterExp, propertyName);
var queryValue = Expression.Constant(query.ToLower(), typeof(string));
var toLowerMethodExp = Expression.Call(propertyExp, toLowerMethod);
var containsMethodExp = Expression.Call(toLowerMethodExp, containsMethod, queryValue);
methodCalls.Add(containsMethodExp);
}
var orExp = methodCalls.Aggregate((left, right) => Expression.Or(left, right));
return Expression.Lambda<Func<T, bool>>(orExp, parameterExp);
}
public static IQueryable<T> Search<T>(this IQueryable<T> query, string property) where T : class, IEntity
{
var filterAttributes = typeof(T).GetCustomAttributes(
typeof(FilterableAttribute), true
).FirstOrDefault() as FilterableAttribute;
if (filterAttributes == null) {
return query;
}
var filterableColumns = filterAttributes.FilterableAttributes;
if (filterableColumns == null || filterableColumns.Count() == 0)
{
return query;
}
if (property == null)
{
return query;
}
return query.Where(GetSearchExpression<T>(filterableColumns, property));
}
}
Decorator (example: both a property of my model, and a nested property):
[Filterable(FilterableAttributes = new string[] {
nameof(Foo),
nameof(Bar) + "." + nameof(Models.MyConnectedModel.Baz)
})]
public partial class MyConnectedModel: IEntity
{
}
Nice question :)
Here's how you can do this:
static Expression<Func<T, bool>> GetExpression<T>(string[] propertyNames, string query)
{
var parameterExp = Expression.Parameter(typeof(T), "category");
MethodInfo containsMethod = typeof(string).GetMethod("Contains", new[] { typeof(string) });
MethodInfo toLowerMethod = typeof(string).GetMethod("ToLower",Type.EmptyTypes);
List<Expression> methodCalls = new List<Expression>();
foreach (string propertyName in propertyNames)
{
var propertyExp = Expression.Property(parameterExp, propertyName);
var queryValue = Expression.Constant(query.ToLower(), typeof(string));
var toLowerMethodExp = Expression.Call(propertyExp, toLowerMethod);
var containsMethodExp = Expression.Call(toLowerMethodExp, containsMethod, queryValue);
methodCalls.Add(containsMethodExp);
}
var orExp = methodCalls.Aggregate((left, right) => Expression.Or(left, right));
return Expression.Lambda<Func<T, bool>>(orExp, parameterExp);
}
And then you can use it like this (query is an IQueryable<MyEntity>)
query=query.Where(GetExpression<MyEntity>(queryableProperties,"SomeValue"));
IMO, it can be surprisingly easy:
IQueryable<T> GenericSearch<T>(this IQueryable<T> items, string query)
{
var queryableProperties = items
.First()
.GetType()
.GetProperties()
.Where(p => p.PropertyType == typeof(string))
.ToList();
return items.Where(i => queryableProperties.Any(p => ((string)p.GetValue(i)).Contains(query)));
}
This searches all properties of type string in a list of items.
It can be a class method, an extension method, a static function, it's easy to understand and works well. It can be extended to Fields or restricted to some interfaces easily.
Adjust to your liking.
What is this error :
The parameter 'd' was not bound in the specified LINQ to Entities
query expression
See details :
private static IEnumerable<T> ConnectToDatabase(IQueryable dbSet, ParameterExpression pe, IEnumerable<Expression> expressions,
string orderby, bool desc)
{
// expressions =
Expression body = null;
if (expressions.Any())
{
foreach (Expression expression in expressions)
{
body = ExpressionExtensions.JoinExpressions(body == null, Expression.Or, body, expression);
}
}
if (body == null)
{
Expression left = Expression.Property(pe, "ID");
Expression right = Expression.Constant(-1);
body = Expression.NotEqual(left, right);
}
IQueryable<T> results;
MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
"Where",
new[] { dbSet.ElementType },
dbSet.Expression,
Expression.Lambda<Func<T, bool>>(body, new[] { pe }));
var ModelType = typeof(T);
pe = Expression.Parameter(ModelType, "x");
var propertyInfoOrderBy = GetPropertyInfo(orderby);
var propertyAccess = Expression.MakeMemberAccess(pe, propertyInfoOrderBy);
var orderByExp = Expression.Lambda(propertyAccess, pe);
MethodCallExpression orderByCallExpression = Expression.Call(
typeof(Queryable),
"OrderBy",
new[] { ModelType, propertyInfoOrderBy.PropertyType },
whereCallExpression,
Expression.Quote(orderByExp));
if (desc)
{
MethodCallExpression resultExp = Expression.Call(
typeof(Queryable),
"OrderByDescending",
new[] { ModelType, propertyInfoOrderBy.PropertyType },
orderByCallExpression,
Expression.Quote(orderByExp));
results = dbSet.Provider.CreateQuery<T>(resultExp);
}
else
{
results = dbSet.Provider.CreateQuery<T>(orderByCallExpression);
}
return results.ToList();
}
expressions :
body :
whereCallExpression :
orderByCallExpression :
error :
JN_News class :
public class JN_News
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string NewsLink { get; set; }
public DateTime PubDate { get; set; }
public string ImageLink { get; set; }
public bool IsDisplay { get; set; }
public Decimal? Rate { get; set; }
public int NewsCategories_ID { get; set; }
public virtual JN_NewsCategories JN_NewsCategories { get; set; }
}
JN_NewsCategories class :
public class JN_NewsCategories
{
public int ID { get; set; }
public string NewsCategoriesFa { get; set; }
public string NewsCategoriesEn { get; set; }
public bool IsGetNews { get; set; }
public virtual ICollection<JN_News> JN_News { get; set; }
public JN_NewsCategories()
{
JN_News = new Collection<JN_News>();
}
}
update :
When I've eliminated the following two statements. Working properly:
my all code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace NewsSiteApk.Data.DynamicSearchLibrary
{
public static class SearchUsingExpression<T> where T : class
{
public static IEnumerable<T> Search(IEnumerable<T> listOfT, string search, string orderBy, bool desc, int pageIndex, int pageSize)
{
listOfT = GetListOfData(listOfT, search, orderBy, desc).Skip((pageIndex - 1) * pageSize).Take(pageSize);
return listOfT;
}
public static int GetCount(IEnumerable<T> listOfT, string search)
{
listOfT = GetListOfData(listOfT, search, "id", true);
return listOfT.Count();
}
private static IEnumerable<T> GetListOfData(IEnumerable<T> listOfT, string search, string orderBy, bool desc)
{
var modelType = typeof(T);
ParameterExpression pe = Expression.Parameter(modelType, "d");
var expressions = new List<Expression>();
if (!string.IsNullOrEmpty(search))
{
expressions.AddRange(GetExpressions(modelType.Name, search, pe));
}
var connectToDatabase = ConnectToDatabase(listOfT.AsQueryable(), pe, expressions, orderBy, desc);
return connectToDatabase;
}
private static IEnumerable<T> ConnectToDatabase(IQueryable dbSet, ParameterExpression pe, IEnumerable<Expression> expressions,
string orderby, bool desc)
{
Expression body = null;
if (expressions.Any())
{
foreach (Expression expression in expressions)
{
body = ExpressionExtensions.JoinExpressions(body == null, Expression.Or, body, expression);
}
}
if (body == null)
{
Expression left = Expression.Property(pe, "ID");
Expression right = Expression.Constant(-1);
body = Expression.NotEqual(left, right);
}
IQueryable<T> results;
MethodCallExpression whereCallExpression = Expression.Call(
typeof(Queryable),
"Where",
new[] { dbSet.ElementType },
dbSet.Expression,
Expression.Lambda<Func<T, bool>>(body, new[] { pe }));
var propertyInfoOrderBy = GetPropertyInfo(orderby);
var propertyAccess = Expression.MakeMemberAccess(pe, propertyInfoOrderBy);
var orderByExp = Expression.Lambda(propertyAccess, pe);
var ModelType = typeof(T);
MethodCallExpression orderByCallExpression = Expression.Call(
typeof(Queryable),
"OrderBy",
new[] { ModelType, propertyInfoOrderBy.PropertyType },
whereCallExpression,
Expression.Quote(orderByExp));
if (desc)
{
MethodCallExpression resultExp = Expression.Call(
typeof(Queryable),
"OrderByDescending",
new[] { ModelType, propertyInfoOrderBy.PropertyType },
orderByCallExpression,
Expression.Quote(orderByExp));
results = dbSet.Provider.CreateQuery<T>(resultExp);
}
else
{
results = dbSet.Provider.CreateQuery<T>(orderByCallExpression);
}
return results.ToList();
}
private static IEnumerable<Expression> GetExpressions(string modelName, string search, ParameterExpression pe)
{
var expressions = new List<Expression>();
var fieldModels = GetFields(modelName);
foreach (FieldModel fieldModel in fieldModels)
{
IEnumerable<Expression> conditionsWithSubModel;
if (fieldModel.NameEn.Contains("[]"))
{
conditionsWithSubModel = GetConditionsWithSubModelList(search, fieldModel.NameEn, pe);
}
else if (fieldModel.NameEn.Contains("."))
{
conditionsWithSubModel = GetConditionsWithSubModel(search, fieldModel.NameEn);
}
else
{
conditionsWithSubModel = GetConditionsWithoutSubModel(search, pe, fieldModel.NameEn);
}
if (conditionsWithSubModel != null)
expressions.AddRange(conditionsWithSubModel);
}
return expressions;
}
private static IEnumerable<Expression> GetConditionsWithoutSubModel(string search, ParameterExpression pe,
string parametr)
{
var expressions = new List<Expression>();
foreach (var splitSeacrh in search.Split(' '))
{
Expression left = Expression.Property(pe, parametr);
Expression right = Expression.Constant(splitSeacrh);
MethodCallExpression conditionExpression = Expression.Call(left, typeof(string).GetMethod("Contains"),
right);
expressions.Add(conditionExpression);
}
return expressions;
}
private static IEnumerable<Expression> GetConditionsWithSubModel(string search,
string parameter)
{
// output.Where(d => d.JN_NewsCategories.NewsCategoriesEn.Contains(""));
var expressions = new List<Expression>();
var strings = parameter.Split('$');
var modelName = strings[0]; // Like : JN_News
var subModelName = strings[1].Split('.')[0];// Like : JN_NewsCategories
var subModelField = strings[1].Split('.')[1];// Like : NewsCategoriesEn
foreach (var splitSeacrh in search.Split(' '))
{
Type modelClass = GetModel(modelName);
Type submodelClass = GetModel(subModelName);
ParameterExpression peSubModel = Expression.Parameter(modelClass, "d");
Expression leftSubModel = Expression.Property(peSubModel, modelClass.GetProperty(subModelName));
Expression ex = Expression.Property(leftSubModel, submodelClass.GetProperty(subModelField));
Expression rightSubModel = Expression.Constant(splitSeacrh);
MethodCallExpression conditionExpressionSubModel = Expression.Call(ex,
typeof(string).GetMethod("Contains"), rightSubModel);
expressions.Add(conditionExpressionSubModel);
}
return expressions;
}
private static IEnumerable<Expression> GetConditionsWithSubModelList(string search, string parameter,
ParameterExpression peModel)
{
parameter = parameter.Replace("[]", string.Empty);
var expressions = new List<Expression>();
var subModelName = parameter.Split('.')[0];
var subModelField = parameter.Split('.')[1];
foreach (var splitSeacrh in search.Split(' '))
{
Type modelClass = GetModel(subModelName);
var subModelProperty = GetPropertyInfo(subModelName);
ParameterExpression peSubModel = Expression.Parameter(modelClass, "d");
Expression leftSubModel = Expression.Property(peSubModel, modelClass.GetProperty(subModelField));
Expression rightSubModel = Expression.Constant(splitSeacrh);
MethodCallExpression conditionExpressionSubModel = Expression.Call(leftSubModel,
typeof(string).GetMethod("Contains"), rightSubModel);
LambdaExpression anyLambdaSubModelForModel = Expression.Lambda(conditionExpressionSubModel, peSubModel);
MethodInfo anyMethodForModel = CreateAnyMethodGeneric(subModelProperty);
Expression lambedaSubModelForExpressionModel = Expression.Property(peModel, subModelProperty);
Expression expression = Expression.Call(anyMethodForModel, lambedaSubModelForExpressionModel,
anyLambdaSubModelForModel);
expressions.Add(expression);
}
return expressions;
}
private static Type GetModel(string name)
{
return (typeof(T).Assembly).GetTypes()
.First(d => string.Equals(d.Name, name, StringComparison.CurrentCultureIgnoreCase));
}
private static PropertyInfo GetPropertyInfo(string name)
{
return typeof(T).GetProperties().First(d => string.Equals(d.Name, name, StringComparison.CurrentCultureIgnoreCase));
}
private static MethodInfo CreateAnyMethodGeneric(PropertyInfo propYekiBeAkhar, string methodName = "Any")
{
return
typeof(Enumerable).GetMethods()
.Single(m => m.Name == methodName && m.GetParameters().Length == 2)
.MakeGenericMethod(propYekiBeAkhar.PropertyType.GenericTypeArguments[0]);
}
private static IEnumerable<FieldModel> GetFields(string modelName)
{
var fieldsFactory = new FieldsFactory();
var fieldModels = fieldsFactory.GetClause(modelName);
return fieldModels;
}
}
}
field factory class :
public class FieldsFactory
{
public List<FieldModel> GetClause(string modelName)
{
var type = typeof(FieldsFactory);
var methodInfos = type.GetMethod("Get" + modelName + "Fields", BindingFlags.NonPublic | BindingFlags.Instance);
var listOfFields = (List<FieldModel>)methodInfos.Invoke(this, null);
return listOfFields;
}
private List<FieldModel> GetJN_NewsCategoriesFields()
{
var fields = new List<FieldModel>
{
new FieldModel
{
NameEn = "NewsCategoriesFa",
},
new FieldModel
{
NameEn = "NewsCategoriesEn",
},
new FieldModel
{
NameEn = "JN_News[].Title",
},
new FieldModel
{
NameEn = "JN_News[].Description",
}
};
return fields;
}
private List<FieldModel> GetJN_NewsFields()
{
var fields = new List<FieldModel>
{
new FieldModel
{
NameEn = "Title",
},
new FieldModel
{
NameEn = "JN_News$JN_NewsCategories.NewsCategoriesFa",
},
new FieldModel
{
NameEn = "JN_News$JN_NewsCategories.NewsCategoriesEn",
}
};
return fields;
}
}
i fooooooooooound. I'm very happy.
Only , I replaced the following method :
private static IEnumerable<Expression> GetConditionsWithSubModel(string search, ParameterExpression pe,
string parameter)
{
// output.Where(d => d.JN_NewsCategories.NewsCategoriesEn.Contains(""));
var expressions = new List<Expression>();
var strings = parameter.Split('$');
var modelName = strings[0];
var subModelName = strings[1].Split('.')[0];
var subModelField = strings[1].Split('.')[1];
foreach (var splitSeacrh in search.Split(' '))
{
Type modelClass = GetModel(modelName);
Type submodelClass = GetModel(subModelName);
ParameterExpression peSubModel = Expression.Parameter(modelClass, "d");
Expression leftSubModel = Expression.Property(peSubModel, modelClass.GetProperty(subModelName));
Expression ex = Expression.Property(leftSubModel, submodelClass.GetProperty(subModelField));
Expression rightSubModel = Expression.Constant(splitSeacrh);
MethodCallExpression conditionExpressionSubModel = Expression.Call(ex,
typeof(string).GetMethod("Contains"), rightSubModel);
expressions.Add(conditionExpressionSubModel);
}
return expressions;
}
with following method :
private static IEnumerable<Expression> GetConditionsWithSubModel(string search, ParameterExpression pe,
string parameter)
{
// output.Where(d => d.JN_NewsCategories.NewsCategoriesEn.Contains(""));
var expressions = new List<Expression>();
var strings = parameter.Split('$');
var modelName = strings[0];
var subModelName = strings[1].Split('.')[0];
var subModelField = strings[1].Split('.')[1];
foreach (var splitSeacrh in search.Split(' '))
{
Type modelClass = GetModel(modelName);
Type submodelClass = GetModel(subModelName);
Expression leftSubModel = Expression.Property(pe, modelClass.GetProperty(subModelName));
Expression ex = Expression.Property(leftSubModel, submodelClass.GetProperty(subModelField));
Expression rightSubModel = Expression.Constant(splitSeacrh);
MethodCallExpression conditionExpressionSubModel = Expression.Call(ex,
typeof(string).GetMethod("Contains"), rightSubModel);
expressions.Add(conditionExpressionSubModel);
}
return expressions;
}