tags:

views:

52

answers:

3

I have this simple 2 lines of following code. It compiles fine but never return results in the datagridview. If I change func to p=> p.PTNT_FIRST_NAME.StartsWith(this.textBox1.Text), it works just fine. What's the problem here?

Func<PATIENT, bool> func = (PATIENT p) => p.PTNT_FIRST_NAME.StartsWith(this.textBox1.Text);
this.dataGridView1.DataSource = dataContext.PATIENTs.Where<PATIENT>(func).Select(q => q);
A: 

Change Func<PATIENT, bool> to Expression<Func<PATIENT, bool>>.

SLaks
What's the root cause of this. Why it compiles in the first place?
It compiles to a normal LINQ call, not LINQ-to-SQL. LINQ-to-SQL is a set of extension methods that take `Expresion<T>`'s, so you need to pass `Expression<T>`'s to them.
SLaks
A: 

Try this:

Expression<Func<PATIENT, bool>> func = (PATIENT p) => p.PTNT_FIRST_NAME.StartsWith(this.textBox1.Text);
Jimmy W
A: 

It works. But what's the cause of the problem? Why it compiles fine and Where does accept Func as parameter.

[This](http://stackoverflow.com/questions/2783807/t-sql-generated-from-linq-to-sql-is-missing-a-where-clause) should give you some insight. Yes, I was the one who asked the question.
Jimmy W