Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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:
C#
var query = Queryable.Where(db.Customers, (Func<Customer, Boolean>)predicate.Compile());

Thanks in advanced.
Code:
C#
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();

        }
    }
}
Posted
Updated 23-Oct-11 17:00pm
v2

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
 
Share this answer
 
Comments
alimahdipour 24-Oct-11 4:16am    
Excuse me SAKryukov , my error is like below:

Error 1 The type arguments for method 'System.Linq.Queryable.Where<tsource>(System.Linq.IQueryable<tsource>, System.Linq.Expressions.Expression<system.func<tsource,int,bool>>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

Actually in the book, I am studying; there is one code for linq to objects like below that is working, just in my previous code i liked to change the code in the book:
1- Linq to sql (northwind) instead of linq to objects.
2- Using text boxes instead of constant value.
3- Using Queryable instead of Enumerable.

Please help what changes I have to do in expression tree.
Thanks a lot.

The code in the book:

var book = Expression.Parameter(typeof(Book), "book");
var titleExpression = Expression.NotEqual(
Expression.Property(book, "Title"),
Expression.Constant("Funny Stories"));
var pageCountExpression = Expression.GreaterThan(
Expression.Property(book, "PageCount"),
Expression.Constant(100));
var andExpression = Expression.And(titleExpression,
pageCountExpression);
var predicate = Expression.Lambda(andExpression, book);
var query = Enumerable.Where(SampleData.Books,
(Func<book, boolean="">)predicate.Compile());
Sergey Alexandrovich Kryukov 24-Oct-11 14:20pm    
OK, this is different story. Shouldn't it be Funk<book>, not Funk<book,>? Still, there is no definition of Compile.
--SA
Espen Harlinn 24-Oct-11 14:15pm    
Good guess :)
Sergey Alexandrovich Kryukov 24-Oct-11 14:16pm    
Thank you, Espen.
--SA
alimahdipour 25-Oct-11 3:26am    
my book is "LINQ in Action" and i do not think this is funk, maybe my question is not clear and understandable because i am new to linq.
thanks.
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:

C#
DataClasses1DataContext db = new DataClasses1DataContext();

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900