SQL as a keyword in Dynamic Linq

I want to use the SQL Like keyword in dynamic LINQ.

The query I want to make is similar to this

select * from table_a where column_a like '%search%'

If column_a can be dynamically changed to another column, etc.

In this dynamic LINQ

var result = db.table_a.Where( a=> (a.column_a.Contains("search")) );

But the column cannot be dynamically changed, only the search key can

How to create dynamic LINQ, for example

var result = db.table_a.Where("column_a == \"search\"");

So that we can dynamically change the column and search key

+3
source share
5 answers

Create an ExtensionMethods Class Using This Function

    public static IQueryable<T> Like<T>(this IQueryable<T> source, string propertyName, string keyword)
    {
        var type = typeof(T);
        var property = type.GetProperty(propertyName);
        string number = "Int";
        if (property.PropertyType.Name.StartsWith(number))
            return source;

        var parameter = Expression.Parameter(type, "p");
        var propertyAccess = Expression.MakeMemberAccess(parameter, property);
        var constant = Expression.Constant("%" + keyword + "%");
        MethodCallExpression methodExp = Expression.Call(null, typeof(SqlMethods).GetMethod("Like", new Type[] { typeof(string), typeof(string) }), propertyAccess, constant);
        Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(methodExp, parameter);
        return source.Where(lambda);
    }

And then name it like this:

var result = db.table_a.Like("column_a", "%search%");
+2
source

This should work for you:

.Where("AColumnName.Contains(@0)", "Criteria") 
+3
source

, SQL LIKE LINQ. , , .

, :

using System.Data.Linq.SqlClient;

if (!string.IsNullOrEmpty(data.MailerName))
    search = search.Where(a => SqlMethods.Like(a.Mailer.Name, string.Format("%{0}%", data.MailerName)));

search - , , data - , , . , , Where search.

0

Maybe a little late, but another approach is to add an extension method that uses Containsto model a keyword Likelike:

public static class DbHelpers
    {
        public static IQueryable<T> Like<T>(this IQueryable<T> source, string propertyName, string propertyValue)
        {
            var prop = typeof(T).GetProperty(propertyName);
            if (prop == null || prop.PropertyType.Name.StartsWith("Int"))
                return source;

            ParameterExpression parameter = Expression.Parameter(typeof(T), "row");
            Expression property = Expression.Property(parameter, propertyName);
            Expression value = Expression.Constant(propertyValue);

            var containsmethod = value.Type.GetMethod("Contains", new[] { typeof(string) });
            var call = Expression.Call(property, containsmethod, value);
            var lambda = Expression.Lambda<Func<T, bool>>(call, parameter);
            return source.Where(lambda);
        }
    }

And using this:

var foo = entity.AsQueryable().Like("Name", "bla bla");

If send is of PropertyNametype int, the method returns the original object that you previously passed to it.

0
source

Source: https://habr.com/ru/post/1742914/


All Articles