<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4428609205848899267</atom:id><lastBuildDate>Sat, 31 Aug 2024 14:01:39 +0000</lastBuildDate><category>ADO.NET</category><category>C#</category><category>CodeProject</category><category>Entity Framework</category><category>LINQ to Entities</category><category>Server Side Paging</category><title>Confessions of a Full-Time Consultant</title><description>All the random thoughts and discoveries that occur while exploring the world of .NET.</description><link>http://ftconsultant.blogspot.com/</link><managingEditor>noreply@blogger.com (&lt;b&gt;Stephen Inglish&lt;/b&gt;)</managingEditor><generator>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4428609205848899267.post-4092892163768376411</guid><pubDate>Tue, 30 Jun 2009 13:32:00 +0000</pubDate><atom:updated>2009-07-13T20:03:54.425-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">ADO.NET</category><category domain="http://www.blogger.com/atom/ns#">C#</category><category domain="http://www.blogger.com/atom/ns#">CodeProject</category><category domain="http://www.blogger.com/atom/ns#">Entity Framework</category><category domain="http://www.blogger.com/atom/ns#">LINQ to Entities</category><category domain="http://www.blogger.com/atom/ns#">Server Side Paging</category><title>Paging with LINQ to Entities</title><description>&lt;a href=&quot;http://www.codeproject.com/script/Articles/BlogFeedList.aspx?amid=4869534&quot; rel=&quot;tag&quot; style=&quot;display:none&quot;&gt;CodeProject&lt;/a&gt;I was in a situation where I found myself adding multiple copies of a query with LINQ to Entities on the code behind of a Page because I could not find a way to build a query inside a method and return that query.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;The Problem:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;Since I was using a GridView and I was doing server side paging on that GridView, I wanted to only return a select number of records from the database.  The first thought was to simply place the repeated code in a method and return the (&lt;span style=&quot;font-size:85%;&quot;&gt;&lt;span style=&quot;font-style: italic;font-family:arial;&quot; &gt;System.Data.Objects.ObjectQuery&lt;/span&gt;&lt;/span&gt;) query back to the caller.&lt;br /&gt;&lt;pre&gt;private ObjectQuery &lt;feattype&gt;QueryDatabase()&lt;br /&gt;{&lt;br /&gt;MyEntities db = new &lt;/feattype&gt;My&lt;feattype&gt;Entities();&lt;br /&gt;&lt;br /&gt;// Define the query to use to return data&lt;br /&gt;var query = from x in db.MyTable&lt;br /&gt;        where (String.IsNullOrEmpty(txtSearchMyName.Text) ||&lt;br /&gt;               x.Name.StartsWith(txtSearchMyName.Text))&lt;br /&gt;        orderby x.Name&lt;br /&gt;        select new&lt;br /&gt;        {&lt;br /&gt;          MyTableID = x.MyTableID,&lt;br /&gt;          Name = x.Name,&lt;br /&gt;          Description = x.Description&lt;br /&gt;        };&lt;br /&gt;return query&lt;feattype&gt;;&lt;br /&gt;}&lt;/feattype&gt;&lt;/feattype&gt;&lt;br /&gt;&lt;br /&gt;protected void btnSearch_Click(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;// Define the query to use, including any search points&lt;br /&gt;var query = BuildQuery();&lt;br /&gt;&lt;br /&gt;// Utilize the query to retrieve the proper page&lt;br /&gt;gvFeats.DataSource = query;&lt;br /&gt;gvFeats.DataBind();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;Compile error.  &quot;Cannot convert anonymous type &amp;lt;a&#39;&amp;gt; ... to Object Query&quot;&lt;br /&gt;&lt;br /&gt;The problem I faced was what was the type that should be returned?  Since we are using the &lt;span style=&quot;color: rgb(51, 51, 255);&quot;&gt;var &lt;/span&gt;keyword, I fired up the debugger to find what type was being returned when I executed my query.  Turns out it was a System.Data.Objects.ObjectQuery&amp;lt;T&amp;gt;, where T being the anonymous type being returned from the select clause in the query.&lt;br /&gt;&lt;br /&gt;Since you cannot return Anonymous types from a method, I thought to replace the anonymous type with the known Entity Framework type created for that table.&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;private IQueryable&amp;lt;MyTable&amp;gt; &lt;feattype&gt;QueryDatabase()&lt;br /&gt;{&lt;br /&gt;MyEntities db = new &lt;/feattype&gt;My&lt;feattype&gt;Entities();&lt;br /&gt;&lt;br /&gt;// Define the query to use to return data&lt;br /&gt;var query = from x in db.MyTable&lt;br /&gt;        where (String.IsNullOrEmpty(txtSearchMyName.Text) ||&lt;br /&gt;               x.Name.StartsWith(txtSearchMyName.Text))&lt;br /&gt;        orderby x.Name&lt;br /&gt;        select new MyTable&lt;br /&gt;        {&lt;br /&gt;          MyTableID = x.MyTableID,&lt;br /&gt;          Name = x.Name,&lt;br /&gt;          Description = x.Description&lt;br /&gt;        };&lt;br /&gt;return query&lt;feattype&gt;;&lt;br /&gt;}&lt;/feattype&gt;&lt;/feattype&gt;&lt;/pre&gt; Runtime error: &quot;&lt;span id=&quot;ctl00_lblErrorMessage&quot; class=&quot;Message&quot;&gt;The entity or complex type  &#39;MyModel.MyTable&#39; cannot be constructed in a LINQ to Entities  query.&lt;/span&gt; &quot; Sigh...&lt;br /&gt;&lt;br /&gt;So, we can&#39;t return an anonymous type, and we can&#39;t build the LINQ entity in the query directly...  Instead of returning the query to the caller to perform server side paging, perform the paging for the caller, and return the results in a List&amp;lt;MyTable&amp;gt;.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;span style=&quot;font-size:130%;&quot;&gt;The Solution:&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Server Side paging with LINQ to Entities.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;private List&lt;feattype&gt; QueryDatabase(int PageSize,&lt;br /&gt;                       int PageIndex,&lt;br /&gt;                       out int Count)&lt;br /&gt;{&lt;br /&gt;MyEntities db = new &lt;/feattype&gt;My&lt;feattype&gt;Entities();&lt;br /&gt;&lt;br /&gt;// Define the query to use to return data&lt;br /&gt;var query = from x in db.MyTable&lt;br /&gt;        where (String.IsNullOrEmpty(txtSearchMyName.Text) ||&lt;br /&gt;               x.Name.StartsWith(txtSearchMyName.Text))&lt;br /&gt;        orderby x.Name&lt;br /&gt;        select new&lt;br /&gt;        {&lt;br /&gt;          MyTableID = x.MyTableID,&lt;br /&gt;          Name = x.Name,&lt;br /&gt;          Description = x.Description&lt;br /&gt;        };&lt;br /&gt;Count = query.Count();&lt;br /&gt;&lt;br /&gt;// Perform server side paging by Skipping and Taking&lt;br /&gt;return query.Skip(PageIndex * PageSize)&lt;br /&gt;        .Take(PageSize)&lt;br /&gt;        .ToList()&lt;br /&gt;        .ConvertAll&lt;feattype&gt;(x =&gt; new MyTable&lt;br /&gt;                   {&lt;br /&gt;                     MyTableID = x.MyTableID,&lt;br /&gt;                     Name = x.Name,&lt;br /&gt;                     Description = x.Description&lt;br /&gt;                   });&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;protected void btnSearch_Click(object sender, EventArgs e)&lt;br /&gt;{&lt;br /&gt;int virtualItemCount;&lt;br /&gt;&lt;br /&gt;// Define the query to use, including any search points&lt;br /&gt;var query = BuildQuery(gvMyTables.PageSize,&lt;br /&gt;                   0,&lt;br /&gt;                   out virtualItemCount);&lt;br /&gt;&lt;br /&gt;// Utilize the query to retrieve the full data count&lt;br /&gt;&lt;/feattype&gt;&lt;/feattype&gt;gvMyTables&lt;feattype&gt;&lt;feattype&gt;.VirtualItemCount = virtualItemCount;&lt;br /&gt;&lt;br /&gt;// Utilize the query to retrieve the proper page&lt;br /&gt;&lt;/feattype&gt;&lt;/feattype&gt;gvMyTables.&lt;feattype&gt;&lt;feattype&gt;DataSource = query;&lt;br /&gt;&lt;/feattype&gt;&lt;/feattype&gt;gvMyTables&lt;feattype&gt;&lt;feattype&gt;.DataBind();&lt;br /&gt;&lt;/feattype&gt;&lt;/feattype&gt;&lt;feattype&gt;&lt;feattype&gt;}&lt;br /&gt;&lt;/feattype&gt;&lt;/feattype&gt;&lt;/pre&gt;Because I am using my &lt;a href=&quot;http://www.codeproject.com/KB/webforms/PagingGridview.aspx&quot;&gt;PagingGridView&lt;/a&gt;, I need to know the total number of records the query can return without any paging (so the PagingGridView will display the correct number of pages), so the method above uses an out parameter to communicate that information to the caller, which passes it to the VirtualItemCount of the PagingGridView.&lt;br /&gt;&lt;br /&gt;I had to define the anonymous type for the query, but immediately convert it to the Entity Framework type to return in the List&amp;lt;MyTable&amp;gt;.&lt;br /&gt;&lt;br /&gt;It is a little more code to type, but at least it works.  Hopefully the Entity Framework will change their policy on not being able to create Complex types with LINQ.</description><link>http://ftconsultant.blogspot.com/2009/06/paging-with-linq-to-entities.html</link><author>noreply@blogger.com (&lt;b&gt;Stephen Inglish&lt;/b&gt;)</author><thr:total>0</thr:total></item></channel></rss>