string id = (from c in context.Users
where c.u_id == Id
select c.u_id).SingleOrDefault();
1)How i can improve the performance for the above using Linq complied query for the above.
We are limited to use .NET 3.5 only.
2)What will be performance gain using a complied linq query for the above code in terms of percentage?
You would create the compiled query via:
You would then use this as:
As for the performance gain, this may be significant, but it may also be minimal. It really depends on how quick the query is being performed, and how often you can reuse the compiled query. In many cases, using the cmopiled query is actually slower, as the overhead of compilation doesn’t make up for the speed gains. You would need to measure to determine if this is worth the effort.
Note that, if you know you only have one unique ID, you can also potentially speed up your original query merely by using
FirstOrDefault()instead ofSingleOrDefault().