【问题标题】:select from a list with field name and field value with linq (similar to select command)使用 linq 从具有字段名称和字段值的列表中选择(类似于 select 命令)
【发布时间】:2015-10-18 09:30:32
【问题描述】:

我想从动态列表中进行选择,其中列表字段名称例如为“SecName”并且 SecName 值等于例如“xxx”, 我知道如何在 sql 中创建它,但我想创建它并将其与实体框架一起使用。 就像在 sql 中执行字符串一样。 我怎样才能在实体框架和 linq 中做到这一点。 应该是这样的

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

我正在寻找通过传递两个字符串来过滤列表的语法,第一个字符串应该是属性名称,另一个应该是该属性值。

【问题讨论】:

标签: c# sql-server linq entity-framework-6


【解决方案1】:

经过搜索,我找到了答案,答案是:

     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;
        }

    }

并像这样使用它:

var lst = _efmodel.Count(2015).AsQueryable()
                                    .Where(
          FilterLinq<PazTedad_Result>.GetWherePredicate(
          new SearchField("FieldName", "FieldValue"))).ToList();

【讨论】:

    【解决方案2】:

    代码一定是这样的?

    var lst=_efmodel.Table.Where(x => x.fieldname == <SomeValue>).ToList();
    

    【讨论】:

    • 它调用linq的通用模板。我不知道您使用过的字段的确切名称。我很想用你帖子中的代码提出建议。
    • 我不确定我的代码是否正确,我可能应该看起来像这样,我正在寻找正确的语法,我想要一个语法来过滤我的列表两个字符串,第一个是列名,第二个是该列值。
    • 如果你这样想,一切都会改变
    猜你喜欢
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2010-11-15
    • 2021-05-01
    相关资源
    最近更新 更多