select from a list with field name and field value with linq (similar to select command)

c# entity-framework-6 linq sql-server

Question

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.

1
2
10/18/2015 1:15:24 PM

Popular Answer

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();
2
10/18/2015 1:43:04 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow