2

I want to select from a dynamic list where the list field name is for example 'SecName'and the SecName value is equal to for example 'xxx', I know how to create it in sql, but I want to create and use it with entity framework. Like Executing String in sql. How can I make it in entity framework and linq. It should be something like this

var lst=_efmodel.Tables.where(x=>x.fieldname=="SecName" && x.value=="xxx").tolist();

I'm looking for syntax to filter my list by passing two string, 1st string should be property Name and the Other should be that property value.

Hootan.Ghiasi
  • 99
  • 2
  • 9
  • 1
    That is not trivial, not maintainable and generally a bad idea. Consider remodelling your database or restructuring your code so you don't need it. It would help if you could show an example of _why_ you need this. Anyway see [How to create an expression at runtime](http://stackoverflow.com/questions/19164308/how-to-create-an-expression-at-runtime-for-use-in-groupby-with-entity-framewor), [Building dynamic where clauses in LINQ to EF queries](http://stackoverflow.com/questions/14901430/), and so on. – CodeCaster Oct 18 '15 at 09:47

2 Answers2

2

After searching I found the Answer ,Answer is:

     public class SearchField
    {
        public string Name { get; set; }
        public string @Value { get; set; }
        //public string Operator { get; set; }

        public SearchField(string Name, string @Value)
        {
            this.Name = Name;
            this.@Value = @Value;
            //Operator = "=";
        }
    }
    public class FilterLinq<T>
    {
        public static Expression<Func<T, Boolean>> GetWherePredicate(params SearchField[] SearchFieldList)
        {

            //the 'IN' parameter for expression ie T=> condition
            ParameterExpression pe = Expression.Parameter(typeof(T), typeof(T).Name);

            //combine them with and 1=1 Like no expression
            Expression combined = null;

            if (SearchFieldList != null)
            {
                foreach (var fieldItem in SearchFieldList)
                {
                    //Expression for accessing Fields name property
                    Expression columnNameProperty = Expression.Property(pe, fieldItem.Name);


                    //the name constant to match 
                    Expression columnValue = Expression.Constant(fieldItem.Value);

                    //the first expression: PatientantLastName = ?
                    Expression e1 = Expression.Equal(columnNameProperty, columnValue);

                    if (combined == null)
                    {
                        combined = e1;
                    }
                    else
                    {
                        combined = Expression.And(combined, e1);
                    }
                }
            }

            //create and return the predicate
            if (combined != null) return Expression.Lambda<Func<T, Boolean>>(combined, pe);
            return null;
        }

    }

and use it like this :

var lst = _efmodel.Count(2015).AsQueryable()
                                    .Where(
          FilterLinq<PazTedad_Result>.GetWherePredicate(
          new SearchField("FieldName", "FieldValue"))).ToList();
Hootan.Ghiasi
  • 99
  • 2
  • 9
0

Code must be something like this?

var lst=_efmodel.Table.Where(x => x.fieldname == <SomeValue>).ToList();
Mykola
  • 3,343
  • 6
  • 23
  • 39
  • Its general template of calling linq. I do not know exact name of fields wich you have used. I am gust making suggestions with code from your post. – Mykola Oct 18 '15 at 13:08
  • I'm Not sure that my code is correct or not, I gust maybe it's should be look like this,I'm looking for the Correct syntax , I want a syntax to filter my list by two string,First is column Name and the second is that column value. – Hootan.Ghiasi Oct 18 '15 at 13:13
  • If you think in that way it, changes everything – Mykola Oct 18 '15 at 13:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92646/discussion-between-hootan-ghiasi-and-mykola). – Hootan.Ghiasi Oct 18 '15 at 13:19