{"id":119,"date":"2010-10-12T17:25:09","date_gmt":"2010-10-12T21:25:09","guid":{"rendered":"http:\/\/wukenny.host-ed.net\/?p=119"},"modified":"2010-10-12T17:25:09","modified_gmt":"2010-10-12T21:25:09","slug":"linq-to-sql-return-a-typed-classstruct","status":"publish","type":"post","link":"http:\/\/kennywu.info\/?p=119","title":{"rendered":"LINQ to SQL return a typed class\/struct"},"content":{"rendered":"<p>The trick is to project the query result into an ordinary named type with an object initializer:<br \/>\n<strong>Paginate<\/strong> is used to demonstrate how the query can be used for further processes.<\/p>\n<pre lang=\"csharp\" escaped=\"true\">using System;\nusing System.Collections;\nusing System.Configuration;\nusing System.Data;\nusing System.Linq;\nusing System.Web;\nusing System.Web.Security;\nusing System.Web.UI;\nusing System.Web.UI.HtmlControls;\nusing System.Web.UI.WebControls;\nusing System.Web.UI.WebControls.WebParts;\nusing System.Xml.Linq;\n\nnamespace LINQPart3_CSharp\n{\n    public partial class _Default : System.Web.UI.Page\n    {\n        protected void Page_Load(object sender, EventArgs e)\n        {\n            int startRow = 10;\n            int takeRow = 20;\n\n            GridView1.DataSource = BindProducts(startRow, takeRow);\n            GridView1.DataBind();\n        }\n\n        private IQueryable\n BindProducts(int startRow, int takeRow)\n        {\n            NorthwindDataContext db = new NorthwindDataContext();\n\n            var products = from p in db.Products\n                           where p.OrderDetails.Count >2\n                           select new prod\n                           {\n                               ID = p.ProductID,\n                               Name = p.ProductName,\n                               NumOrders = p.OrderDetails.Count,\n                               Revenue = String.Format(\"{0:C}\", p.OrderDetails.Sum(o =&gt; o.UnitPrice * o.Quantity))\n                           };\n            \/\/return products.Skip(startRow).Take(takeRow);\n            return Paginate(products, startRow, takeRow);\n        }\n\n        public IQueryable Paginate(IQueryable query, int skip, int take)\n        {\n            return query.Skip(skip).Take(take);\n        }\n\n        struct prod {\n            public int ID { get; set; }\n            public string Name { get; set; }\n            public int NumOrders { get; set; }\n            public string Revenue { get; set; }\n        }\n    }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The trick is to project the query result into an ordinary named type with an object initializer: Paginate is used to demonstrate how the query can be used for further processes. using System; using System.Collections; using System.Configuration; using System.Data; using &hellip; <a href=\"http:\/\/kennywu.info\/?p=119\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[40],"class_list":["post-119","post","type-post","status-publish","format-standard","hentry","category-database","category-programing","tag-linq"],"_links":{"self":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=119"}],"version-history":[{"count":0,"href":"http:\/\/kennywu.info\/index.php?rest_route=\/wp\/v2\/posts\/119\/revisions"}],"wp:attachment":[{"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=119"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kennywu.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}