{"id":2853,"date":"2011-07-15T07:36:17","date_gmt":"2011-07-15T07:36:17","guid":{"rendered":"http:\/\/jellywp.com\/theme\/entron\/demo5\/?p=2853"},"modified":"2011-07-15T07:36:17","modified_gmt":"2011-07-15T07:36:17","slug":"linq-to-sharepoint-examples","status":"publish","type":"post","link":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/linq-to-sharepoint-examples\/","title":{"rendered":"LinQ to SharePoint examples"},"content":{"rendered":"<p><strong>Namespaces to refer in Visual studio for using LinQ<\/strong><br \/>\nusing System.Linq;<br \/>\nusing Microsoft.SharePoint.Linq;<br \/>\nusing System.Collections.Generic;<\/p>\n<p><strong>Generate Entity classes using SPMetal<\/strong><br \/>\nRun the following command to generate it<br \/>\nspmetal \/web:http:\/\/ \/code:<\/p>\n<p><strong>Creating a Join between two list UserAccounts and Invoices<\/strong><br \/>\nList UserAccounts have Account, Name<br \/>\nList Invoices have Account, Amount, Paid<\/p>\n<p><span style=\"color: green;\">\/\/First refer the Datacontext class<\/span><br \/>\nXDataContext a = new XDataContext(SPContext.Current.Web.Url);<\/p>\n<p><span style=\"color: green;\">\/\/Refere the Lists and create entityLists out of them<\/span><\/p>\n<p>EntityList Invoices = a.GetList(&#8220;Invoices&#8221;) ;<br \/>\nEntityList UserAccounts = a.GetList(&#8220;UserAccounts&#8221;);<\/p>\n<p><span style=\"color: green;\">\/\/need to do a join from the two lists and bring them together.<\/span><br \/>\n<span style=\"color: green;\">\/\/use the ToList() on the list in the from to avoid getting errors due to inefficient CAML queries. with this method it retrives data from the list instantaniously.<\/span><\/p>\n<p>var query=<br \/>\n(from u in UserAccounts.ToList()<br \/>\njoin i in Invoices on u.Account equals i.Account<br \/>\nwhere int.Parse(u.Account.ToString()) &gt; 1001 &amp;&amp; int.Parse(u.Title.ToString()) &lt; 10010<br \/>\nselect new { u.Name, u.Account, i.Amount, i.Paid }) ;<\/p>\n<p><span style=\"color: green;\">\/\/make this as the datasource.<\/span>GridView1.DataSource = query;<br \/>\nGridView1.DataBind();<\/p>\n<p><strong>Inserting in Lists using LinQ <\/strong><\/p>\n<p>XDataContext a = new XDataContext(SPContext.Current.Web.Url);<\/p>\n<p>EntityList Invoices = a.GetList(&#8220;Invoices&#8221;);<\/p>\n<p>int count = 0;<br \/>\n<span style=\"color: green;\">\/\/get the count of the records<\/span><br \/>\ncount =<br \/>\n(from x in Invoices<br \/>\nselect x).Count();<\/p>\n<p>List ic = new List();<\/p>\n<p>if (count == 0)<br \/>\n{<br \/>\n<span style=\"color: green;\">\/\/insert 5K records<\/span><br \/>\nfor (int i = 0; i &lt; 300; i++)<br \/>\n{<br \/>\n<span style=\"color: green;\">\/\/create records and store them in a system.collection.generic.list collection<\/span><br \/>\nInvoicesItem inv = new InvoicesItem() { Account = &#8220;100&#8221; + i.ToString(), Amount = 100 + i, Paid = check(i) };<br \/>\nic.Add(inv);<br \/>\n}<\/p>\n<p><span style=\"color: green;\">\/\/insert all of them in shot<\/span><br \/>\nInvoices.InsertAllOnSubmit(ic);<br \/>\na.SubmitChanges();<br \/>\n}<\/p>\n<p><span style=\"color: green;\">\/\/here is the check function that is being used<\/span><br \/>\nprivate Boolean check(int i)<\/p>\n<p>{<br \/>\nif (i % 2 == 0)<br \/>\nreturn false;<br \/>\nelse<br \/>\nreturn true;<br \/>\n}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Namespaces to refer in Visual studio for using LinQ using System.Linq; using Microsoft.SharePoint.Linq; using System.Collections.Generic; Generate Entity classes using SPMetal Run the following command to generate it spmetal \/web:http:\/\/ \/code: Creating a Join between two list UserAccounts and Invoices List UserAccounts have Account, Name List Invoices have Account, Amount, Paid \/\/First refer the Datacontext class [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/posts\/2853"}],"collection":[{"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/comments?post=2853"}],"version-history":[{"count":0,"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/posts\/2853\/revisions"}],"wp:attachment":[{"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/media?parent=2853"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/categories?post=2853"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/teamwp.mydevfactory.com\/abhisek-saha\/aqltech\/wp-json\/wp\/v2\/tags?post=2853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}