Creating a LINQ Select expression dynamically from string column names - c#

I have a method which looks something like this:
public string GetColVal(string aAcctField, bool callM1, bool callM2)
{
if (callM1)
{
// create select clause here to select a string column with name
// equal to the value of aAcctField from Table1
// Expression<Func<Table1, string>> selector = ?
return M1(selector);
}
if (callM2)
{
// create select clause here to select a string column with name
// equal to the value of aAcctField from Table2
// Expression<Func<Table2, string>> selector = ?
return M2(selector);
}
}
And M1() is something like this:
public string M1(Expression<Func<Table1, string>> columnToSelect, string xType)
{
// other logic
string acct = _cntx.Where(c => c.Type == xType)
.Select(columnToSelect)
.FirstOrDefault();
return acct;
}
M2() is also something similar. Please note that the methods are over-simplified. And the methods M1() and M2() work perfectly. I can invoke them this way:
// MyColumn is a strongly typed column in Table1
string acct = M1(x => x.MyColumn, "some value");
But, inside the method GetColVal() how do I construct the select clauses? The comments regarding selector will help you understand what I intend to do. So, please go ahead and read the comment.
I have tried this:
public string GetColVal(string aAcctField, bool callM1, bool callM2)
{
if (callM1)
{
// create select clause here to select a string column with name
// equal to the value of aAcctField from Table1
Expression<Func<Table1, string>> selector = (w) => w.GetType().GetProperty(aAcctField).Name;
return M1(selector);
}
...
}
and I get the exception:
LINQ to Entities does not recognize the method
'System.Reflection.PropertyInfo GetProperty(System.String)' method,
and this method cannot be translated into a store expression
I have looked at these:
Create dynamic LINQ expression for Select with FirstOrDefault inside
Linq access property by variable
Get property value from string using reflection in C#
and many others.
but none of them is quite something like what I need.

Basically you need to use the Expression class methods like Expression.Lambda, Expression.PropertyOrField etc. to build the desired selector like:
static Expression<Func<T, TValue>> MemberSelector<T, TValue>(string name)
{
var parameter = Expression.Parameter(typeof(T), "item");
var body = Expression.PropertyOrField(parameter, name);
return Expression.Lambda<Func<T, TValue>>(body, parameter);
}
To support nested properties, change var body = ... line to
var body = name.Split('.').Aggregate((Expression)parameter, Expression.PropertyOrField);
Sample usage:
if (callM1)
{
return M1(MemberSelector<Table1, string>(aAcctField));
}
...

Related

Entity Framework - How to reference a column name using a string variable

I have a table called "Account." Account has 3 columns: id, acct_name, is_privileged.
When I write something like "account.", visual studio provides me with a list of attributes/methods I can use. Hence, I get the option of using account.id, account.acct_name, and account.is_privileged.
However, I would like to change a particular column's value dynamically, without typing in my column's name. I am getting the column's name dynamically as a string variable. Is it possible to achieve it? If so, how?
My code is as follows:
set_col_name = rowRule.Cells["setcolumnnameDataGridViewTextBoxColumn"].Value.ToString();
set_col_value = rowRule.Cells["setcolumnvalueDataGridViewTextBoxColumn"].Value.ToString();
foreach (DataGridViewRow rowAcc in dgvAccount.Rows)
{
if (isComparable(rowAcc.Cells[col_name].Value.ToString(), comp_operator, col_value))
{
account.id = (int)rowAcc.Cells["idDataGridViewTextBoxColumn2"].Value;
using (ae = new AccountEntities())
{
var temp = ae.Accounts.SingleOrDefault(a => a.id == account.id);
temp.is_privileged = set_col_value; //learn how to do this dynamically
ae.SaveChanges();
}
}
}
Where I do temp.is_privileged, I'd like to achieve something like, temp."set_col_name" = set_col_value;
Instead of specifying the column name directly as being "is_privileged" in this case, I'd like to pass a string to specify it.
Thank you.
If I understand your problem statement correctly, you want something like this to work:
Account temp = // with temp coming from a library such as EntityFramework
temp.SetValue(set_col_name, set_col_value);
this is quite easy to achieve with either pure reflection or Linq Expression Trees (which I opted for):
static class Ext
{
public static void Set<T, TProperty>(this T instance, string propertyName, TProperty value)
{
var instanceExpression = Expression.Parameter(typeof(T), "p");
var propertyGetterExpression = Expression.PropertyOrField(instanceExpression, propertyName);
//generate setter
var newValueExpression = Expression.Parameter(typeof(TProperty), "value");
var assignmentExpression = Expression.Assign(propertyGetterExpression, newValueExpression);
var lambdaExpression = Expression.Lambda<Action<T, TProperty>>(assignmentExpression, instanceExpression, newValueExpression);
var setter = lambdaExpression.Compile();// the generated lambda will look like so: (p, value) => p.{your_property_name} = value;
setter(instance, value);
}
}
one advantage of this method over pure reflection is that you can build the setter delegate once and call it multiple times at later stage (I will leave this with you to experiment)
with the above in place, hopefully you should be able to do something like this:
var set_col_name = "is_privileged";
var set_col_value = true;
using (ae = new AccountEntities())
{
var temp = ae.Accounts.SingleOrDefault(a => a.id == account.id);
temp.Set(set_col_name, set_col_value);
temp.Set("acct_name", "test");
ae.SaveChanges();
}
You need some reflection in this one. For example
public static void CopyValues<T>(T obj1, T obj2)
{
var type = typeof(T);
foreach (var prop in type.GetProperties())
{
prop.SetValue(obj1, prop.GetValue(obj2));
}
}
And use the above function like this:
var source = new Accounts(){is_privileged = false};
var destiny = new Accounts();
CopyValues(source, destiny);
It depends of what you are loking for, but the key is to use REFLECTION!

C# Dynamic Linq: Implement "Like" in The Where Clause

So I want to make a general sorter for my data. I have this code to get data from the database which will extract the data only which contains value.
using System.Linq.Dynamic;
public static IQueryable<object> SortList(string searchString, Type modelType,
IQueryable<object> model)
{
....
string toStringPredicate = type == typeof(string) ? propertyName +
".Contains(#0)" : propertyName + ".ToString().Contains(#0)";
model = model.Where(propertyName + " != NULL AND " + toStringPredicate, value);
}
The model is this:
public class ManageSubscriberItems
{
public int? UserId { get; set; }
public string Email { get; set; }
public Guid SubscriberId { get; set; }
}
When I call:
models = (IQueryable<ManageSubscriberItems>)EcommerceCMS.Helpers.FilterHelper
.SortList(searchString, typeof(ManageSubscriberItems), models);
if(models.Any())
It throws this error:
"LINQ to Entities does not recognize the method 'System.String
ToString()' method, and this method cannot be translated into a store
expression."
EDIT
I found the problem, but I still cannot fix it. So if the property is not string, it will throw an error when calling .ToString().Contains().
model = model.Where(propertyName + " != NULL AND " + propertyName +
".ToString().Contains(#0)", value);
What I want is to implement LIKE in the query. Can anyone help me?
If you use System.Linq.Dynamic.Core with EF Core, you have an option to use
var q = context.Cars.Where(config, "DynamicFunctions.Like(Brand, \"%a%\")");
See this link for an example:
https://github.com/StefH/System.Linq.Dynamic.Core/blob/6fc7fcc43b248940560a0728c4d181e191f9eec1/src-console/ConsoleAppEF2.1.1/Program.cs#L117
And I just tested in linqpad connecting to a real database, and code like this just works?
var result1 = Entity1s.Where("Url != NULL AND it.Url.Contains(#0)", "e");
[UPDATE 2019-04-17]]
In case you don't know the type, you can cast it to object and then cast that to a string.
Code:
var r = Entity1s.Select("string(object(Rating))").Where("Contains(#0)", "6");
so the problem here is that IQueryable thing happens on the SQL server not in C#... so SQL server doesn't know anything about .toString() method.
so => and Like operator it self works on strings.. so it's nvarchar and varchar data types in SQL server.
I could give You an example of how to achieve it if you could tell me more about your problem and what You want to achieve.
could do a sample.
You already have a "Like" in Linq that can run in the database and works with strings, it's called "IndexOf":
((IQueryable)model).Where(m => m.Property.IndexOf(searchString) == 1);
According to MSDN:
IndexOf(string)
'The zero-based index position of value if that string is found, or -1 if it is not. If value is Empty, the return value is 0.'
So I want to make a general sorter for my data.
instead of fixing 'invoke issue', general way should use generics, like
public static IQueryable<T> OrderBy<T>(this IQueryable<T> source,
string property,
bool asc = true) where T : class
{
//STEP 1: Validate MORE!
var searchProperty = typeof(T).GetProperty(property);
if (searchProperty == null) throw new ArgumentException("property");
....
//STEP 2: Create the OrderBy property selector
var parameter = Expression.Parameter(typeof(T), "o");
var selectorExpr = Expression.Lambda(Expression.Property(parameter, property), parameter)
//STEP 3: Update the IQueryable expression to include OrderBy
Expression queryExpr = source.Expression;
queryExpr = Expression.Call(typeof(Queryable), asc ? "OrderBy" : "OrderByDescending",
new Type[] { source.ElementType, searchProperty.PropertyType },
queryExpr,
selectorExpr);
return source.Provider.CreateQuery<T>(queryExpr);
}
having property name string and direction usually used for making 'column sorting' on data.
Next are relation predicates are coming, and 'Linq.Dynamic' seems reasonable when doing from scratch, but there is generic and canonical form exists.

Return value of property based on property name

How do I build expression tree in C# that returns value of a property based on the name of the property
Func<Foo, long> getValue(string propertyName)
{
// i think that the beginning of the expression tree would look like this
// but i'm not sure this is correct
var inputParameter = Expression.Parameter(typeof(Foo));
var desiredProperty = typeof(Foo).GetProperty(propertyName);
var valueOfProperty = Expression.Property(inputParameter, desiredProperty);
// ... ??? todo: expression that returns value
}
Call to this function looks like this which is part of another expression that is passed to Linq's Select method:
value = getValue("Bar").Invoke(FooInstance)
Should be enough:
var lambda = Expression.Lambda<Func<Foo, long>>(valueOfProperty, inputParameter);
return lambda.Compile();
Anyway - what's the purpose for building Expression when you could get value directly via reflection?
return someFoo => (long)desiredProperty.GetValue(someFoo);

Dynamic Linq query on relationship with foreign key of type Guid

I'm using System.Linq.Dynamic to query an IQueryable datasource dynamically using a where-clause in a string format, like this:
var result = source.Entities.Where("City = #0", new object[] { "London" });
The example above works fine. But now I want to query on a foreign key-property of type Guid like this:
var result = source.Entities.Where("CompanyId = #0", new object[] { "838AD581-CEAB-4B44-850F-D05AB3D791AB" });
This won't work because a Guid can't be compared to a string by default. And I have to provide the guid as a string because it's originally coming from a json-request and json does not support guid's.
Firstly, is this even a correct way of querying over a relationship or is there an other syntax for doing this?
Secondly, how do I modify Dynamic.cs from the Dynamic Linq-project to automatically convert a string to guid if the entity-property being compared with is of type guid?
You have many ways for solving. Simplest, as i think, will be change your query like this
var result = source.Entities.Where("CompanyId.Equals(#0)", new object[] { Guid.Parse("838AD581-CEAB-4B44-850F-D05AB3D791AB") });
If you want use operators = and == then in Dynamic.cs you need change interface IEqualitySignatures : IRelationalSignatures like this
interface IEqualitySignatures : IRelationalSignatures
{
....
F(Guid x, Guid y);
....
}
after that you can use next query
var result = source.Entities.Where("CompanyId=#0", new object[] { Guid.Parse("838AD581-CEAB-4B44-850F-D05AB3D791AB") });
OR
var result = source.Entities.Where("CompanyId==#0", new object[] { Guid.Parse("838AD581-CEAB-4B44-850F-D05AB3D791AB") });
But if you want use string parameter you need change ParseComparison method in ExpressionParser class. You need add yet another checking for operand types like this
....
//you need add this condition
else if(left.Type==typeof(Guid) && right.Type==typeof(string)){
right = Expression.Call(typeof(Guid).GetMethod("Parse"), right);
}
//end condition
else {
CheckAndPromoteOperands(isEquality ? typeof(IEqualitySignatures) : typeof(IRelationalSignatures), op.text, ref left, ref right, op.pos);
}
....
and then you query will be work
var result = source.Entities.Where("CompanyId = #0", new object[] { "838AD581-CEAB-4B44-850F-D05AB3D791AB" });

Retrieving an Expression from a property and adding it to an expression tree

I've tried to simplify this example, as the actual code I'm playing with is more complex. So while this example may seem silly, bear with me. Let's say I'm working with the AdventureWorks database and I decide I want to add a property called Blarg to the Product table that returns an expression that contains code I would like to use in several places:
public partial class Product
{
public Expression<Func<Product, string>> Blarg
{
get { return product => product.ProductModelID.HasValue ? "Blarg?" : "Blarg!"; }
}
}
What I want to do is create an expression expression tree, have it get the Expression from Product.Blarg, and group by the result. Something like this:
var productParameter = Expression.Parameter(typeof(Product), "product");
// The Problem
var groupExpression = Expression.Lambda<Func<Product, string>>(
Expression.Invoke(
Expression.Property(productParameter, "Blarg"),
productParameter),
productParameter);
using (AdventureWorksDataContext db = new AdventureWorksDataContext())
{
var result = db.Products.GroupBy(groupExpression).ToList();
// Throws ArgumentException: "The argument 'value' was the wrong type.
// Expected 'System.Delegate'.
// Actual 'System.Linq.Expressions.Expression`1[System.Func`2[LINQ_Test.Product,System.String]]'."
}
Obviously groupExpression is incorrect (see the code comment for the exception), but I'm not sure how I should be doing it. What I thought I was saying is "get the Expression from the product.Blarg, execute it, and return the string result." I guess that's not what I'm actually saying there, though. I'm still trying to figure out expression trees. Any idea how I could pull this off?
When you call Expression.Invoke, the first argument must be an existing LambdaExpression - it can't be an Expression to a LambdaExpression. Or in other words: it isn't going to evaluate Product.Blarg per row and use a different sub-expression each time.
Instead, you would retrieve this lambda first, perhaps making it static and accessing it via reflection if you only know it by name:
var lambda = (LambdaExpression) typeof(Product)
.GetProperty("Blarg").GetValue(null,null);
And pass lambda in as the argument to Expression.Invoke; here's a fully working LINQ-to-Objects example showing this (via AsQueryable()):
using System;
using System.Linq;
using System.Linq.Expressions;
public partial class Product
{
public static Expression<Func<Product, string>> Blarg
{
get { return product => product.ProductModelID.HasValue ? "Blarg?" : "Blarg!"; }
}
public int? ProductModelID { get; set; }
static void Main()
{
var lambda = (LambdaExpression)typeof(Product)
.GetProperty("Blarg").GetValue(null, null);
var productParameter = Expression.Parameter(typeof(Product), "product");
// The Problem
var groupExpression = Expression.Lambda<Func<Product, string>>(
Expression.Invoke(
lambda,
productParameter),
productParameter);
var data = new[] {
new Product { ProductModelID = 123},
new Product { ProductModelID = null},
new Product { ProductModelID = 456},
};
var qry = data.AsQueryable().GroupBy(groupExpression).ToList();
}
}
var qry = data.AsQueryable().GroupBy(Blarg).ToList();
That works, same as Marc's code.
Note: Blarg is already correct, there is no reason to 're-invoke' it.

Categories