2

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);
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
user341450
  • 21
  • 1

2 Answers2

2

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

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • What's the root cause of this. Why it compiles in the first place? – user341450 May 14 '10 at 17:09
  • 1
    It compiles to a normal LINQ call, not LINQ-to-SQL. LINQ-to-SQL is a set of extension methods that take `Expresion`'s, so you need to pass `Expression`'s to them. – SLaks May 14 '10 at 17:49
2

Try this:

Expression<Func<PATIENT, bool>> func = (PATIENT p) => p.PTNT_FIRST_NAME.StartsWith(this.textBox1.Text);
Jimmy W
  • 539
  • 3
  • 11