austinc
austinc

Reputation: 285

linq to entities contains does not work

I am new to linq to entities and I have a problems in using the contain method. Here is the sample table in my DB.

DataTable UserTable = new DataTable();
UserTable .Columns.Add("ID", typeof(int));
UserTable .Columns.Add("Name", typeof(string));

UserTable .Rows.Add(1,"Tom");
UserTable .Rows.Add(2,"Tom_dad");
UserTable .Rows.Add(3,"Tom_mom");
UserTable .Rows.Add(4,"Tom_sister");

In Linq to entities I wrote the statement

entities edm = new entities();
var query= from a in edm .UserTable 
           where a.Name.Contains("Tom")
           select a;

The result is only first row where the name is exactly Tom.

How can I get results that included 4 rows? Such as SQL LIKE method %Tom%.

Thanks for your help and kindness.

Environments : VS2010 / .Net FrameWork4

//Update//

Thanks a lot to Ben Millane. It works after I add the ToList() behind the statement.

var query= (from a in edm .UserTable 
               where a.Name.Contains("Tom")
               select a).ToList();

What is the difference between the two statements? I wander why this improvement can work.

Upvotes: 1

Views: 3930

Answers (1)

BenM
BenM

Reputation: 4278

Try

entities edm = new entities();
var query= (from a in edm .UserTable 
       where a.Name.Contains("Tom")
       select a).ToList();

Does that work?

Upvotes: 3

Related Questions