We formerly used reflection to create linq queries, e.g. for the GetById method:
private IQueryable<T> GetQueryById(TKey id)
{
var query = _dbset; //DbSet<T>
var keyNames = _context.Model
.FindRuntimeEntityType(typeof(T))
.FindPrimaryKey()
.Properties
.Select(x => x.Name)
.ToList();
if (keyNames.Count() == 1)
{
query = query.Where(e => e.GetType().GetProperty(keyNames[0]).GetValue(e, null)
.Equals(id)); //throws error
}
return query;
}
This does not seem to work any more in EF Core 3:
The LINQ expression 'DbSet
.Where(c => c.GetType().GetProperty(__get_Item_0).GetValue(
obj: c,
index: null).Equals((object)__id_1))' could not be translated.
Is it possible to rewrite the query so that EF Core does not complain?
You have to create predicate dynamically:
private IQueryable<T> GetQueryById(TKey id)
{
IQueryable<T> query = _dbset; //DbSet<T>
var keyNames = _context.Model
.FindRuntimeEntityType(typeof(T))
.FindPrimaryKey()
.Properties
.Select(x => x.Name)
.ToList();
if (keyNames.Count == 1)
{
var keyExpression = Expression.Constant(id);
var entityParam = Expression.Parameter(typeof(T), "e");
var body = Expression.Equal(Expression.PropertyOrField(entityParam, keyNames[0]), keyExpression);
var predicate = Expression.Lambda<Func<T, bool>>(body, entityParam);
query = query.Where(predicate);
}
else
// better to throw exception
throw new Exception($"Cannot find entity key.");
return query;
}
Related
Is it possible to get elements in a sqlite table from a string with the name of the table (using Entity Framework)? How?
And how can I get only the value of a property? (I need to get a list of IDs to create a in html that's used to choose which element in a table the user wants to delete)
using using Microsoft.EntityFrameworkCore;
public static List<string> GetAllIdsFromTableName(string tableName)
{
var db = new dbContext();
// What I would like to do:
// return db.tableName.Select(x => x.id).ToList<string>();
}
The following extension returns IQueryable<string> and you can materialise arrays, lists, or you can do it asynchronously:
var result = context.GetAllIdsFromTable("SomeTable", "Id")
.ToList();
And implementation:
public static class QueryableExtensions
{
private static readonly MethodInfo _toStringMethod = typeof(Convert).GetMethods()
.Single(m =>
m.Name == nameof(Convert.ToString) && m.GetParameters().Length == 1 &&
m.GetParameters()[0].ParameterType == typeof(object)
);
public static IQueryable<string> GetAllIdsFromTable(this DbContext ctx, string tableName, string idColumnName = "Id")
{
var model = ctx.Model;
var entityType = model.GetEntityTypes().FirstOrDefault(et =>
tableName.Equals(et.GetTableName(), StringComparison.InvariantCultureIgnoreCase));
if (entityType == null)
throw new InvalidOperationException($"Entity for table '{tableName}' not found.");
// GetColumnName() can be obsolete, it depends on EF Core version.
var prop = entityType.GetProperties().FirstOrDefault(p =>
idColumnName.Equals(p.GetColumnName(), StringComparison.InvariantCultureIgnoreCase));
if (prop == null)
throw new InvalidOperationException($"Property for column '{tableName}'.'{idColumnName}' not found.");
var entityParam = Expression.Parameter(entityType.ClrType, "e");
var ctxParam = Expression.Parameter(typeof(DbContext), "ctx");
// ctx.Set<entityType>()
var setQuery = Expression.Call(ctxParam, nameof(DbContext.Set), new[] { entityType.ClrType });
Expression propExpression;
if (prop.PropertyInfo == null)
// 'prop' is Shadow property, so call via EF.Property(e, "name")
propExpression = Expression.Call(typeof(EF), nameof(EF.Property), new[] { prop.ClrType },
entityParam, Expression.Constant(prop.Name));
else
propExpression = Expression.MakeMemberAccess(entityParam, prop.PropertyInfo);
propExpression = EnsureString(propExpression);
// e => e.Prop
var propLambda = Expression.Lambda(propExpression, entityParam);
// ctx.Set<entityType>().Select(e => e.Prop)
Expression selectAll = Expression.Call(typeof(Queryable), nameof(Queryable.Select),
new[] { entityType.ClrType, typeof(string) },
setQuery, Expression.Quote(propLambda));
var constructQuery = Expression.Lambda<Func<DbContext, IQueryable<string>>>(selectAll, ctxParam);
return constructQuery.Compile()(ctx);
}
private static Expression EnsureString(Expression expression)
{
if (expression.Type == typeof(string))
return expression;
if (expression.Type != typeof(object))
expression = Expression.Convert(expression, typeof(object));
expression = Expression.Call(_toStringMethod, expression);
return expression;
}
}
Working on a asp net core 2.2 application. I want to dynamic order a query result.
this is the code I have:
public IActionResult OnGetRecords(int pagenum, int pagesize, string sortDataField, string sortOrder)
{
sortOrder = sortOrder ?? "asc";
var Mut = from M in _DB.Mutations
join S in _DB.Shifts on M.ShiftId equals S.ShiftId
join U in _DB.RoosterUsers on M.UserId equals U.RoosterUserId
select new MutationModel
{
MutId=M.MutationId,
Naam=U.FirstName + " " + U.LastName,
UserId=M.UserId,
MutationType =S.publicName,
DateVan=M.DateStartOn,
DateTot=M.DateTill
};
if (sortDataField != null)
{
if (sortOrder == "asc")
{
Mut = Mut.OrderBy(m => m.GetType().GetProperty(sortDataField).GetValue(m, null));
}
else
{
Mut = Mut.OrderByDescending(m => m.GetType().GetProperty(sortDataField).GetValue(m, null));
}
}
int total = Mut.Count();
var Tresult = Mut.Skip(pagenum * pagesize).Take(pagesize);
var uit = new
{
TotalRows = total,
Rows = Tresult
};
return new JsonResult(uit);
}
}
But it is not working, when I try to order on a field, the line:
Mut = Mut.OrderBy(m => m.GetType().GetProperty(sortDataField).GetValue(m, null));
is not giving an error but is returning an result with out records.
Is EF core different from the 'old' EF in this?
somebody knows how to do this in EF Core
m => m.GetType().GetProperty(sortDataField).GetValue(m, null)
Is not a valid expression for OrderByin this case that can be translated into valid SQL for EF to execute
You will need to use the sortDataField to build an expression dynamically to use with the OrderBy calls.
The following is done as an extension method for convenience
public static Expression<Func<TModel, object>> GetPropertyExpression<TModel>(this IEnumerable<TModel> model, string propertyName) {
// Manually build the expression tree for
// the lambda expression m => m.PropertyName.
// (TModel m) =>
var parameter = Expression.Parameter(typeof(TModel), "m");
// (TModel m) => m.PropertyName
var property = Expression.PropertyOrField(parameter, propertyName);
// (TModel m) => (object) m.PropertyName
var cast = Expression.Convert(property, typeof(object));
var expression = Expression.Lambda<Func<TModel, object>>(cast, parameter);
return expression;
}
It builds up the expression tree for sorting that can then be used like
if (sortDataField != null) {
//m => m.sortDataField
var keySelector = Mut.GetPropertyExpression(sortDataField);
if (sortOrder == "asc") {
Mut = Mut.OrderBy(keySelector);
} else {
Mut = Mut.OrderByDescending(keySelector);
}
}
to order the query
Sorry to be late with this. Thanks for your help. But the problem was something different. In Asp.net.core you send the result via return new JsonResult(uit); to the client.
The problem is that capitalized field names are changed to non capitalized names. so If you send them back to the server again with an Ajax call you have to Capitalize them again!
I try to perform a simple LIKE action on the database site, while having query building services based on generic types. I found out while debugging however, that performing EF.Functions.Like() with reflection does not work as expected:
The LINQ expression 'where __Functions_0.Like([c].GetType().GetProperty("FirstName").GetValue([c], null).ToString(), "%Test%")' could not be translated and will be evaluated locally..
The code that makes the difference
That works:
var query = _context.Set<Customer>().Where(c => EF.Functions.Like(c.FirstName, "%Test%"));
This throws the warning & tries to resolve in memory:
var query = _context.Set<Customer>().Where(c => EF.Functions.Like(c.GetType().GetProperty("FirstName").GetValue(c, null).ToString(), "%Test%"));
Does the Linq query builder or the EF.Functions not support reflections?
Sorry if the questions seem basic, it's my first attempt with .NET Core :)
In EF the lambdas are ExpressionTrees and the expressions are translated to T-SQL so that the query can be executed in the database.
You can create an extension method like so:
public static IQueryable<T> Search<T>(this IQueryable<T> source, string propertyName, string searchTerm)
{
if (string.IsNullOrEmpty(propertyName) || string.IsNullOrEmpty(searchTerm))
{
return source;
}
var property = typeof(T).GetProperty(propertyName);
if (property is null)
{
return source;
}
searchTerm = "%" + searchTerm + "%";
var itemParameter = Parameter(typeof(T), "item");
var functions = Property(null, typeof(EF).GetProperty(nameof(EF.Functions)));
var like = typeof(DbFunctionsExtensions).GetMethod(nameof(DbFunctionsExtensions.Like), new Type[] { functions.Type, typeof(string), typeof(string) });
Expression expressionProperty = Property(itemParameter, property.Name);
if (property.PropertyType != typeof(string))
{
expressionProperty = Call(expressionProperty, typeof(object).GetMethod(nameof(object.ToString), new Type[0]));
}
var selector = Call(
null,
like,
functions,
expressionProperty,
Constant(searchTerm));
return source.Where(Lambda<Func<T, bool>>(selector, itemParameter));
}
And use it like so:
var query = _context.Set<Customer>().Search("FirstName", "Test").ToList();
var query2 = _context.Set<Customer>().Search("Age", "2").ToList();
For reference this was the Customer I used:
public class Customer
{
[Key]
public Guid Id { get; set; }
public string FirstName { get; set; }
public int Age { get; set; }
}
Simple answer, no.
EntityFramework is trying to covert your where clause in to a SQL Query. There is no native support for reflection in this conversation.
You have 2 options here. You can construct your text outside of your query or directly use property itself. Is there any specific reason for not using something like following?
var query = _context.Set<Customer>().Where(c => EF.Functions.Like(c.FirstName, "%Test%"));
Keep in mind that every ExpresionTree that you put in Where clause has to be translated into SQL query.
Because of that, ExpressionTrees that you can write are quite limited, you have to stick to some rules, thats why reflection is not supported.
Image that instead of :
var query = _context.Set<Customer>().Where(c => EF.Functions.Like(c.GetType().GetProperty("FirstName").GetValue(c, null).ToString(), "%Test%"));
You write something like:
var query = _context.Set<Customer>().Where(c => EF.Functions.Like(SomeMethodThatReturnsString(c), "%Test%"));
It would mean that EF is able to translate any c# code to SQL query - it's obviously not true :)
I chucked together a version of the accepted answer for those using NpgSQL as their EF Core provider as you will need to use the ILike function instead if you want case-insensitivity, also added a second version which combines a bunch of properties into a single Where() clause:
public static IQueryable<T> WhereLike<T>(this IQueryable<T> source, string propertyName, string searchTerm)
{
// Check property name
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentNullException(nameof(propertyName));
}
// Check the search term
if(string.IsNullOrEmpty(searchTerm))
{
throw new ArgumentNullException(nameof(searchTerm));
}
// Check the property exists
var property = typeof(T).GetProperty(propertyName);
if (property == null)
{
throw new ArgumentException($"The property {typeof(T)}.{propertyName} was not found.", nameof(propertyName));
}
// Check the property type
if(property.PropertyType != typeof(string))
{
throw new ArgumentException($"The specified property must be of type {typeof(string)}.", nameof(propertyName));
}
// Get expression constants
var searchPattern = "%" + searchTerm + "%";
var itemParameter = Expression.Parameter(typeof(T), "item");
var functions = Expression.Property(null, typeof(EF).GetProperty(nameof(EF.Functions)));
var likeFunction = typeof(NpgsqlDbFunctionsExtensions).GetMethod(nameof(NpgsqlDbFunctionsExtensions.ILike), new Type[] { functions.Type, typeof(string), typeof(string) });
// Build the property expression and return it
Expression selectorExpression = Expression.Property(itemParameter, property.Name);
selectorExpression = Expression.Call(null, likeFunction, functions, selectorExpression, Expression.Constant(searchPattern));
return source.Where(Expression.Lambda<Func<T, bool>>(selectorExpression, itemParameter));
}
public static IQueryable<T> WhereLike<T>(this IQueryable<T> source, IEnumerable<string> propertyNames, string searchTerm)
{
// Check property name
if (!(propertyNames?.Any() ?? false))
{
throw new ArgumentNullException(nameof(propertyNames));
}
// Check the search term
if (string.IsNullOrEmpty(searchTerm))
{
throw new ArgumentNullException(nameof(searchTerm));
}
// Check the property exists
var properties = propertyNames.Select(p => typeof(T).GetProperty(p)).AsEnumerable();
if (properties.Any(p => p == null))
{
throw new ArgumentException($"One or more specified properties was not found on type {typeof(T)}: {string.Join(",", properties.Where(p => p == null).Select((p, i) => propertyNames.ElementAt(i)))}.", nameof(propertyNames));
}
// Check the property type
if (properties.Any(p => p.PropertyType != typeof(string)))
{
throw new ArgumentException($"The specified properties must be of type {typeof(string)}: {string.Join(",", properties.Where(p => p.PropertyType != typeof(string)).Select(p => p.Name))}.", nameof(propertyNames));
}
// Get the expression constants
var searchPattern = "%" + searchTerm + "%";
var itemParameter = Expression.Parameter(typeof(T), "item");
var functions = Expression.Property(null, typeof(EF).GetProperty(nameof(EF.Functions)));
var likeFunction = typeof(NpgsqlDbFunctionsExtensions).GetMethod(nameof(NpgsqlDbFunctionsExtensions.ILike), new Type[] { functions.Type, typeof(string), typeof(string) });
// Build the expression and return it
Expression selectorExpression = null;
foreach (var property in properties)
{
var previousSelectorExpression = selectorExpression;
selectorExpression = Expression.Property(itemParameter, property.Name);
selectorExpression = Expression.Call(null, likeFunction, functions, selectorExpression, Expression.Constant(searchPattern));
if(previousSelectorExpression != null)
{
selectorExpression = Expression.Or(previousSelectorExpression, selectorExpression);
}
}
return source.Where(Expression.Lambda<Func<T, bool>>(selectorExpression, itemParameter));
}
I have below query :
var result = _workOrders.GroupBy(row => row.Asset, (k, g) => new AverageBetweenTwoFailureViewModel
{
// fields
}).ToList();
and know I need to set field dynamicly in row => row.Asset dynamicly .
I searched on the Net and I found below codes :
var arg = Expression.Parameter(typeof(Item), "item");
var body = Expression.Property(arg, "D");
var lambda = Expression.Lambda<Func<Item, DateTime>>(body, arg);
var keySelector = lambda.Compile();
but that override that I used in Groupby it's different.
I'm using Visual Studio 2015, Entity Framework 6, and trying to build a LINQ expression to fetch results based on a dynamic WHERE clause. The user can choose to search on employeeId, securityId (which is a string), or lastName. For last name, it should do a case-insensitive search, so user can enter upper or lowercase searchValue.
Here's what I've got:
public async Task<ObservableCollection<EmployeeViewModel>>
SearchEmployeesAsync(string selectedColumn, string searchValue)
{
var paramEmployee = Expression.Parameter(typeof(Employee), "e");
Func<EmployeeBase, bool> comparison = null;
if (selectedColumn.Equals("employeeId"))
{
var employeeId = -1;
int.TryParse(searchValue, out employeeId);
comparison = Expression.Lambda<Func<Employee, bool>>(
Expression.Equal(
Expression.Property(paramEmployee, selectedColumn),
Expression.Constant(employeeId)),
paramEmployee).Compile();
}
else
{
comparison = Expression.Lambda<Func<Employee, bool>>(
Expression.Equal(
Expression.Property(paramEmployee, selectedColumn),
Expression.Constant(searchValue)),
paramEmployee).Compile();
}
using (var context = new MyEntities())
{
var query = (from e in context.Employees
.Where(comparison)
select new EmployeeViewModel
{
// Populate view model from entity object here
});
return await Task.Run(() => new ObservableCollection<EmployeeViewModel>(query));
}
}
How do I change the above code to make comparison to be case-insensitive for searches on securityId or lastName (both are strings on the database)? securityId or lastName are covered by the else block above. I'm also open to refactoring the code if there's a better way. One thing I don't want to do is use a third-party library to write dynamic WHERE clauses.
Thank you.
If you want the filtering to be applied in the database and not in the memory, it's essential to use Expression<Func<Employee, bool>> in Where clause rather than Func<Employee, bool> as in your code. The case insensitive comparison can be simulating by using ToLower method.
Also, as others mentioned it would be better to eliminate Task.Run call by using ToListAsync method from System.Data.Linq.QueryableExtensions.
With that being said, the implementation could be like this:
using System.Data.Entity;
public async Task<ObservableCollection<EmployeeViewModel>>
SearchEmployeesAsync(string selectedColumn, string searchValue)
{
var parameter = Expression.Parameter(typeof(T), "e");
Expression left = Expression.PropertyOrField(parameter, selectedColumn);
object value = searchValue;
if (selectedColumn == "employeeId")
{
var employeeId = -1;
int.TryParse(searchValue, out employeeId);
value = employeeId;
}
else
{
// case insensitive
left = Expression.Call(left, "ToLower", Type.EmptyTypes);
value = searchValue.ToLower();
}
var comparison = Expression.Lambda<Func<T, bool>>(
Expression.Equal(left, Expression.Constant(value)),
parameter);
using (var context = new MyEntities())
{
var query = context.Employees
.Where(comparison)
.Select(e => new EmployeeViewModel
{
// Populate view model from entity object here
});
var result = await query.ToListAsync();
return new ObservableCollection<EmployeeViewModel>(result);
}
}
I would recommend doing it the easy way. And I question the Task.Run, but since that wasn't part of the question, I left it alone.
public async Task<ObservableCollection<EmployeeViewModel>>
SearchEmployeesAsync(string selectedColumn, string searchValue)
{
using (var context = new MyEntities())
{
var query = context.Employees.AsQueryable();
switch(selectedColumn)
{
case "employeeId":
var employeeId = -1;
int.TryParse(searchValue, out employeeId);
query = query.Where(e=>e.employeeId == employeeId);
break;
case "lastName":
query = query.Where(e=>e.lastName == searchValue);
break;
case "securityId":
query = query.Where(e=>e.securityId == searchValue);
break;
}
query = query.Select(e=> new EmployeeViewModel
{
// Populate view model from entity object here
});
return await Task.Run(() => new ObservableCollection<EmployeeViewModel>(query));
}
}