LINQ to Entities, Workarounds on what is not supported
In my previous post I talked about what is not supported in LINQ to Entities and made a simple comparison with LINQ to SQL show that these unsupported things works fine in LINQ to SQL.
Here I am going to provide workarounds these things. First one will be wrapping the entities with Business Classes. And the second way will be using client evaluation which is converting to work with LINQ to Object. Before you go further, I recommend that you read my previous post to save your time.
More...
LINQ to Entities, what is not supported?
Recently and after .Net 3.5 SP1 & Visual Studio.Net 2008 SP1 released, I started to play with LINQ to Entities. Really Entity Framework is interesting. Maybe not the best ORM tool, but still it is interesting and easy to use plus it is all one IDE at the end.
At the same time I was watching screen-casts made by Rob Conery about Building MVC Storefront along with TDD. In fact he was using LINQ to SQL. And I liked his coding techniques presented in his sample application and screen-casts.
I decided to play around with LINQ to Entities with MVC. Here I am going to talk about my adventures with LINQ to Entities, and really it is completely different from working with LINQ to SQL.
My recommendation for every developer is to start by reading Supported and Unsupported Methods (LINQ to Entities). Yes it will save you sometime. Do You know LINQ to SQL? Then you are familiar with LINQ. But you need to know what there and what is not there in LINQ to Entities. This even Might help you to build a wish list for LINQ to Entities also ;O).
Here I'm going to use some code samples presented in MVC Storefront and show some of the techniques that will not work with LINQ to Entities. And I will compare code with LINQ to SQL. In some other post, I'm going to present some other workarounds provided by ADO.NET Team when I asked them about the issues I have with LINQ to Entities. Also I'll present my own workarounds. And I'll compare the Generate SQL statements. I am not query optimizer, so I will not say what is the best choice. But you can help me in that.
More...
Building Custom Paging with LINQ, ListView, DataPager and ObjectDataSource, Different Paging Method
Yesterday, I was reading Efficient Paging in SQL Server 2005 post by Justin Etheredge, then in the comments I found Kevin Hazzard pointing to his Efficient LINQ to SQL custom paging approach. I liked his way, which reflects Justin's method which I already seen before but never apply it. I think it is more efficient than my method I provided in an earlier post. And if you googled about custom paging with LINQ to SQL you'll find few more implementation.
Today, I'm updating my method and provide the same sample I made last month with Kevin's method. I made an extension method to IQueryable Interface and called it KavinPage. You can view a demo here
Here is the code for the Extension Method:
1: public static IQueryable<T> KevinPage<T, TResult>(this IQueryable<T> obj, int page, int pageSize, System.Linq.Expressions.Expression<Func<T, TResult>> keySelector, bool asc, out int rowsCount)
2: { 3: rowsCount = obj.Count();
4: if (asc)
5: { 6: return obj.OrderBy(keySelector).Skip(page * pageSize).Take(pageSize);
7: }
8: else
9: { 10: return obj.OrderByDescending(keySelector).Skip(page * pageSize).Take(pageSize);
11: }
12: }
It is all about Extension Methods and how to use them to build SQL Queries. You can return for Kevin's post for detailed explanation. Also you can return to my previous post about this subject for more details about how to put it all together to build Paging ASP.NET ListView using LINQ to SQL and ObjectDataSource.
Download the updated sample.

Building Custom Paging with LINQ, ListView, DataPager and ObjectDataSource
Last week I posted about building custom paging with LINQ to SQL. And I wrapped the functionality with Extension Method to IQueryable<T> Interface.
Today I'm going to put the custom paging in a practical sample using ASP.NET ListView, DataPager and ObjectDataSource Control. Click here to download the sample. View demo here.
The sample is using Northwind Database. So first I created a Northwind LINQ to SQL class ".dbml"
More...
Building Custom Paging and Sorting Queries with LINQ to SQL and wrap it in Extension Method
Introduction:
Since my early days with web application and I used to observe people writing different techniques for paging data coming from the database. Many articles where written, and different techniques for different databases. All that I am talking about paging data on the database before returning results to the application which will renders it to the client.
One Technique:
You might be familiar with queries like this one:
1: SELECT TOP (10) [R1].[ProductID], [R1].[ProductName]
2: FROM (
3: SELECT TOP (77) [R0].[ProductID], [R0].[ProductName]
4: FROM [dbo].[Products] AS [R0]
5: ORDER BY [R0].[ProductID] DESC
6: ) AS [R1]
7: ORDER BY [R1].[ProductID]
The above query is one of the techniques used for paging data on the database side. When wrapping this query with a stored procedure it will look like this:
1: CREATE PROCEDURE [dbo].[usp_Product_GETPAGE]
2: (
3: @page int,
4: @pagelength int,
5: @sortfield varchar(100),
6: @descending bit,
7: @rowcount int output
8: )
9: AS
10: BEGIN
11: SET NOCOUNT OFF
12: DECLARE @Err int
13: SELECT @rowcount = COUNT(*) from [Products]
14: declare @innerrows int
15: declare @sortdesc varchar(100)
16: declare @sortasc varchar(100)
17: declare @a varchar(6)
18: declare @b varchar(6)
19: IF @descending=0
20: BEGIN
21: set @a = ' DESC '
22: set @b = ' ASC '
23: END
24: ELSE
25: BEGIN
26: set @a = ' ASC '
27: set @b = ' DESC '
28: END
29: IF charindex(@sortfield, ' [ProductID]') > 0
30: BEGIN
31: set @sortdesc = ''
32: set @sortasc = ''
33: END
34: ELSE
35: BEGIN
36: set @sortdesc = ', [ProductID] ' + @a
37: set @sortasc = ', [ProductID] ' + @b
38: END
39: set @innerrows = @rowcount - (@page * @pagelength)
40: DECLARE @sql nvarchar(1000)
41: SET @sql = 'SELECT TOP ' + STR(@pagelength) + ' [ProductID], [ProductName] FROM
42: (
43: SELECT TOP ' + STR(@innerrows) + ' [ProductID],
44: [ProductName]
45: FROM
46: [Products]
47: ORDER BY [Products].' + @sortfield + @a + @sortdesc + '
48: ) Alias
49: ORDER BY Alias.' + @sortfield + @b + @sortasc
50: EXEC (@sql);
51: END
The above procedure is somehow complex because it support paging and that is why it build the SQL statement and use EXEC function.
I'll not discuss its performance; my target is to discuss how to convert this query into LINQ query using extension methods then generalize it to be as an Extension Method for IQueryable Interface.
LINQ Equivalent:
The LINQ Equivalent is very simple using Extension Methods of the IQueryable and IOrderedQueryable interfaces. The following code shows that:More...