LINQ Expression в наборе данных - PullRequest
1 голос
/ 11 октября 2011

У меня возникают проблемы при передаче выражения в наборе данных, где условие .. Я вставляю все свои коды .. Пожалуйста, исправьте меня, если я не прав, и предложите, если есть короткие методы ..

код для проверки (возраст> 10) && (пол == 'M'), мне нужно применить это правило в наборе данных.

  Expression<Func<int, bool>> ageexpr; // = creteriaattributeIDtest => creteriaattributeIDtest < Convert.ToInt32(rightval1);

         ParameterExpression numparam = Expression.Parameter(typeof(int), "age");
         ConstantExpression criteriaValue1 = Expression.Constant(Convert.ToInt32(rightval1), typeof(int));
         BinaryExpression comparison1 = Expression.LessThan(numparam, criteriaValue1);

         ageexpr = Expression.Lambda<Func<int, bool>>(
                 comparison1,
                new ParameterExpression[] { numparam });

         Func<int, bool> resultage = ageexpr.Compile();

         // Invoking the delegate and writing the result to the console.

         bool firstrule = resultage(14);

         Console.WriteLine("1st Rule" + firstrule);

        // DataView res1 = dt1.AsEnumerable().Where(resultage(Convert.ToInt32(rightval1))).AsDataView();


         Expression<Func<string, bool>> genexpr;

         ParameterExpression genparam = Expression.Parameter(typeof(string), "gender");
         ConstantExpression criteriaValue2 = Expression.Constant("M", typeof(string));
         BinaryExpression comparison2 = Expression.Equal(genparam, criteriaValue2);

         genexpr = Expression.Lambda<Func<string, bool>>(
                 comparison2,
                new ParameterExpression[] { genparam });

         Func<string, bool> resultgen = genexpr.Compile();

         bool secondrule = resultgen("M");

         // Invoking the delegate and writing the result to the console.
         Console.WriteLine("2nd Rule" + secondrule);


         Expression finexpr = Expression.AndAlso(Expression.Constant(firstrule), Expression.Constant(secondrule));
         Console.WriteLine(Expression.Lambda<Func<bool>>(finexpr).Compile()());

1 Ответ

0 голосов
/ 11 октября 2011

Не совсем понятно, в чем заключается ваша проблема, но если вы просто хотите создать DataView через LINQ, почему бы не использовать что-то вроде этого:

    static void Main(string[] args)
    {
        var minimunAge = 10;

        var t = new DataTable();
        t.Columns.Add("age", typeof(Int32));
        t.Columns.Add("gender", typeof(string));
        t.Columns.Add("name", typeof(string));

        t.Rows.Add(20, "M", "Steve");
        t.Rows.Add(5, "M", "John");
        t.Rows.Add(32, "F", "Mary");

        var view = t.AsEnumerable().Where(r => r.Field<Int32>("age") > minimunAge && r.Field<string>("gender") == "M").AsDataView();
    }

Если вы действительно хотите использовать выражения, попробуйте это:

    static void Main(string[] args)
    {
        var t = new DataTable();
        t.Columns.Add("age", typeof(Int32));
        t.Columns.Add("gender", typeof(string));
        t.Columns.Add("name", typeof(string));

        t.Rows.Add(20, "M", "Steve");
        t.Rows.Add(5, "M", "John");
        t.Rows.Add(32, "F", "Mary");

        Expression<Func<int, bool>> ageexpr;

        ParameterExpression numparam = Expression.Parameter(typeof(int), "age");
        ConstantExpression criteriaValue1 = Expression.Constant(Convert.ToInt32(10), typeof(int));
        BinaryExpression comparison1 = Expression.GreaterThan(numparam, criteriaValue1);

        ageexpr = Expression.Lambda<Func<int, bool>>(
                comparison1,
               new ParameterExpression[] { numparam });

        Func<int, bool> resultage = ageexpr.Compile();

        // Invoking the delegate and writing the result to the console.

        bool firstrule = resultage(14);

        Console.WriteLine("1st Rule" + firstrule);

        DataView res1 = t.AsEnumerable().Where(r=> resultage(r.Field<Int32>("age"))).AsDataView();
    }

Вы должны передать параметр своим делегатам.

...