ASR
ASR

Reputation: 429

EntityFramework Linq query

I cannot understand what is wrong with following query:

var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp;

Should this be same as:

select * from mytable
where Barcode like '%11%' and Description like '%EW%';

If I run this in sql server i get four rows as it should be, but not when I run the linq query i get 0 rows.

Can please someone help me. It is rather a simple query and it is giving me such a headache. Thank you very much

Upvotes: 0

Views: 227

Answers (3)

Ammar
Ammar

Reputation: 1068

Run a query with only one condition? " c.Barcode.Contains("11") ".

That code should run fine.

Upvotes: 0

StriplingWarrior
StriplingWarrior

Reputation: 156459

dbset.AsEnumerable<mytable>()...

Don't do that!

You are causing all your data to get pulled from the database before checking the Where condition.

Other than that, it's not really clear why your query isn't working. Your syntax is correct. I'm guessing the data doesn't actually look like you think it does. Either that, or you're expecting like %EW% to match something in a case-insensitive manner and since the Where clause is being evaluated by LINQ to Objects you're not getting that behavior.

Upvotes: 2

Saeed Amiri
Saeed Amiri

Reputation: 22555

You forget fetch data, do this:

var tmp = dbset.AsEnumerable<mytable>().Where(
c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();

Also do not call AsEnumerable soon, use it as below:

var tmp = ctx.mytable.Where(
                  c=>c.Barcode.Contains("11") && c.Description.Contains("EW") );
return tmp.ToList();

Upvotes: 3

Related Questions