0

I'm using EF5 for ObjectQuery,

var query = this.ObjectContext.CreateQuery<Role>("SELECT VALUE n FROM Role as n");
query = query.Where("it.Name like '%@name%'", new ObjectParameter("name", name));

But it doesn't work, which always return all results. Any ideas? Thx a lot!

iNc0ming
  • 383
  • 1
  • 6
  • 17

1 Answers1

2

You want to use .Contains. When used on a string column translates to Sql LIKE

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • Thx, But in objectquery there's no 'Contains' command,could you give me a code example? – iNc0ming Dec 05 '12 at 04:48
  • This is in Linq to Entities. In E-Sql you would use like - as per msdn doc http://msdn.microsoft.com/en-us/library/bb399359.aspx. Check what sql query is created for your query (you probably could use .ToTraceString()). Also you did not tell what the value of the parameter is - if it is empty string for instance you would probably select everything. Btw. why do you use E-SQL? – Pawel Dec 05 '12 at 05:50
  • according to the ef performance consideration http://msdn.microsoft.com/en-us/data/hh949853.aspx, I think ESQL will have better performance compared to Linq. – iNc0ming Dec 05 '12 at 06:10
  • In EF 5.0 (i.e. on .NET Framework 4.5) and EF6 (.NET Framework 4 and .NET Framework 4.5) Linq queries are automatically compiled and cached. In EF4 you could compile queries manually. The performance of compiled Linq queries and E-SQL queries should be similar. – Pawel Dec 05 '12 at 06:18
  • I see. I think I'll switch to Linq queries on ObjectContext.Anyway EF6 is still on Alpha state.By the way, how can I set MergeOption.NoTracking for linq queries? – iNc0ming Dec 05 '12 at 06:21
  • If you are using DbContext you would use .AsNoTracking() on you DbQuery. On ObjectQuery you would use MergeOption property - I think it should be pretty much the same as for E-Sql queries. – Pawel Dec 05 '12 at 06:38