表达式树问题 [英] expression tree problem

查看:86
本文介绍了表达式树问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好
我用2个文本框和1个按钮和1个网格视图制作了一页.然后,我将下面的代码用于页面的按钮单击.现在,当我在文本框中输入一些值时,例如在textbox1中输入"France",在textbox2中输入"Paris",则什么也不会发生,也没有结果.
此行也有错误:

Hello
I made one page with 2 text boxes and 1 button and 1 grid view. Then I used the below code for button click of the page. Now when I enter some value in text boxes for example "France" in textbox1 and "Paris" in textbox2 nothing will be happen and I have no result.
Also there is error for this line:

var query = Queryable.Where(db.Customers, (Func<Customer, Boolean>)predicate.Compile());


在此先谢谢您.
代码:


Thanks in advanced.
Code:

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
       

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext db = new DataClasses1DataContext();
            db.Log = Console.Out;
            var customer = Expression.Parameter(typeof(Customer), "customer");
            var countryExpression = Expression.Equal(
Expression.Property(customer,"country"),
Expression.Constant(TextBox1.Text));
            var cityExpression = Expression.Equal(
Expression.Property(customer, "City"),
Expression.Constant(TextBox2.Text));
            var andExpression = Expression.And(countryExpression,
cityExpression);
            var predicate = Expression.Lambda(andExpression, customer);
            var query = Queryable.Where(db.Customers, (Func<Customer, Boolean>)predicate.Compile());

            GridView1.DataSource = query;
            GridView1.DataBind();

        }
    }
}

推荐答案

在不告知错误消息的情况下说此行有错误"是不好的.为什么,使回答变得更困难?为什么又来了.

但是,在这种特殊情况下,很显然,您正在尝试对(Func<customer,>)predicate.Compile()进行大小写处理,即,一个不带Customer谓词谓词的参数的函数,那么您怎么可能期望它呢?上班? predicate的声明未显示.

至于其余的,这个想法还不清楚,显示的代码是不完整的.如果您不解释期望的结果以及为什么需要这种结果",那么我没有结果"是不够的.

—SA
This is not nice to say "there is error for this line" without telling the error message. Why, to make answering harder? Why, again.

Nevertheless, in this particular case it''s apparent, that you''re trying to case (Func<customer,>)predicate.Compile(), that is, a function with not arguments to a predicate of one argument of Customer, so how can you possible expect it to work? The declaration of predicate is not shown.

As to the rest of it, the idea is just not clear, and the shown code is incomplete. "I have no results" is not enough if you don''t explain what results did you expect and why did you need such "results".

—SA


如果您已经勾选了将生成的对象名称进行复数或单数化,则可以通过访问客户列表客户对象.因此,现在,如果您执行这样的操作,您可能会达到所需的目标:

If you had already checked the Pluralize or singularize generated object names you have access to the customers list by Customers object. So now if you do something like this you will probably get to what you want:

DataClasses1DataContext db = new DataClasses1DataContext();

GridView1.DataSource = db.Customers.Where(i=> i.country == textbox1.Text && i.City == textbox2.Text);
GridView1.DataBind();


这篇关于表达式树问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆