Corey Roth [MVP] : C# 3.0http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/C_2300_+3.0/default.aspxTags: C# 3.0enCommunityServer 2007.1 (Build: 20917.1142)My 500th Post! Left Outer Joins with LINQhttp://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/10/15/my-500th-post-left-outer-joins-with-linq.aspxWed, 15 Oct 2008 19:43:46 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:743CoreyRoth2http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=743http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/10/15/my-500th-post-left-outer-joins-with-linq.aspx#comments<p>Today, this blog reaches an important milestone.&nbsp; This is my 500th post.&nbsp; It&#39;s just a number, but I thought I would use it as an opportunity to talk about the history of the blog and where it is heading.&nbsp; On December 1st, 2004, I wrote my first .NET Tip of the Day about a great new method on primitives called <a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2004/12/01/tryparse-is-a-good-time.aspx">TryParse</a> in the upcoming release of ASP.NET 2.0.&nbsp; This blog started out as a simple announcements list built on a SharePoint intranet site so that I could educate a team of developers on emerging technology (at the time it was .NET 2.0 and SQL Server 2005).&nbsp; When I left that company, I wanted to keep the concept going so I exported all of the content and created a home-grown blogging engine at <a href="http://www.dotnettipoftheday.com">dotnettipoftheday.com</a>.&nbsp; Since then, I have been committed to giving back to the community by providing useful information that will help developers do their every day jobs.&nbsp; My blog has always focused on how to do something not to give my opinion on something that I may or may not know anything about.&nbsp; Admittedly, I haven&#39;t been able to keep up with the daily thing of posting (although I did a pretty good job back then), but I still make an effort to post a few things a week.</p> <p>Sometime in 2007, the term DotNetMafia was coined by the godfather himself <a href="http://www.dotnetmafia.com/blogs/kylekelin/default.aspx">Kyle Kelin</a>.&nbsp; Although the original idea for the mafia has evolved, it turned out to be a great way to get blogs from several of us onto one web site, <a href="http://www.dotnetmafia.com/">dotnetmafia.com</a>, which ran on DotNetNuke.&nbsp; The site worked ok for us for a while, but it was lacking.&nbsp; Later that year, I imported everything (well almost everything) into a new Community Server site where we are today.&nbsp; In case you haven&#39;t seen the rest of the DotNetMafia bloggers, be sure and check out <a href="http://www.dotnetmafia.com/blogs/anthonykilhoffer/default.aspx">Tony Kilhoffer</a>, <a href="http://www.dotnetmafia.com/blogs/jamesashley/default.aspx">James Ashley</a>, <a href="http://www.dotnetmafia.com/blogs/coryrobinson/default.aspx">Cory Robinson</a>, and <a href="http://www.dotnetmafia.com/blogs/kevin/default.aspx">Kevin Williams</a>.&nbsp; They post on a variety of topics (including some outside of .NET).</p> <p>In nearly 4 years, I have brought you 500 posts.&nbsp; 112 of those posts have been on SharePoint.&nbsp; Obviously you can see that I have shifted focus some.&nbsp; The main reason is I blog about the things I am working with on a daily basis.&nbsp; Right now, that has been SharePoint.&nbsp; Although I still find time to blog about LINQ and other things from time to time.&nbsp; Let&#39;s face it though, the SharePoint community needs all the help it can get.&nbsp; Sites like <a href="http://www.sharepointblogs.com">SharePointBlogs.com</a> (which this feed is syndicated too), have made a huge difference in helping people find the right information.&nbsp; This is why I am committed to posting every nuance I find in the product, so that the next SharePoint developer coming along doesn&#39;t have to spend time trying to figure it out.</p> <p>I am pleased with how the site has grown to date.&nbsp; In four years, we have gone from having a couple of internal users a month, to 200 - 300 unique visitors a day.&nbsp; 65% of that traffic each day comes from search.&nbsp; The rest are from RSS subscriptions, referrals, and my <a>CodePlex project</a>.&nbsp; For now, I plan to keep the content coming and encourage others to blog too.&nbsp; I am committed to helping the community even more and hope to continue to grow the site and organize meetups.&nbsp; I want to thank all of my friends, all two of my readers, <a href="http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/">Copy Source as HTML</a>, and everyone that has left a comment.&nbsp; I really appreciate it.</p> <p>Enough fluff for now though, let&#39;s get to some content.&nbsp; I am going to step away from SharePoint for today&#39;s post and try to hit a broader audience talking about LINQ.</p> <h3>Left Outer Joins with LINQ</h3> <p>The topic I am talking about today is doing left outer joins with LINQ.&nbsp; Doing a join in LINQ is pretty simple, however doing an outer join is a bit more tricky because the way you do it doesn&#39;t follow the way you would think of it using T-SQL.&nbsp; The documentation on LINQ covers how to do this, but it doesn&#39;t do a great job explaining what you are doing and why.&nbsp; For this example let&#39;s say we have two classes <em>Product</em> and <em>Inventory</em>.&nbsp; The Product class has the following properties Name, ProductId, and Price.&nbsp; The Inventory class has the properties ProductId and Count.&nbsp; We want to get a list of all products and know how many we have in inventory but unfortunately the Inventory table isn&#39;t complete and it doesn&#39;t have data on all products.&nbsp; We still want the product to return in a query though.</p> <p>First, for the purpose of example, I populate two lists.&nbsp; This could just as easily come from an XML document or SQL.&nbsp; In this case we have three products and two items containing inventory, but info is missing for one particular product.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Product</span>&gt; productList = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Product</span>&gt;();</p> <p style="margin:0px;">productList.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">Product</span>() { Name = <span style="color:#a31515;">&quot;Product 1&quot;</span>, Price = 10.99f, ProductId = 2 });</p> <p style="margin:0px;">productList.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">Product</span>() { Name = <span style="color:#a31515;">&quot;Product 2&quot;</span>, Price = 1.99f, ProductId = 5 });</p> <p style="margin:0px;">productList.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">Product</span>() { Name = <span style="color:#a31515;">&quot;Product 3&quot;</span>, Price = 3.99f, ProductId = 9 });</p> <p style="margin:0px;">&nbsp;</p> <p style="margin:0px;"><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Inventory</span>&gt; inventoryList = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">Inventory</span>&gt;();</p> <p style="margin:0px;">inventoryList.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">Inventory</span>() { ProductId = 2, Count = 54 });</p> <p style="margin:0px;">inventoryList.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">Inventory</span>() { ProductId = 9, Count = 31 });</p></div> <p>If this was a T-SQL query it would probably look something like this:</p> <p><em>SELECT p.Name, i.Count FROM Products p LEFT OUTER JOIN Inventory i ON p.ProductId = i.ProductId</em></p> <p>In LINQ it is going to look a bit different though.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">var</span> productInventory = <span style="color:blue;">from</span> product <span style="color:blue;">in</span> productList</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">join</span> inventory <span style="color:blue;">in</span> inventoryList</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">on</span> product.ProductId <span style="color:blue;">equals</span> inventory.ProductId <span style="color:blue;">into</span> productInventoryGroup</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">from</span> item <span style="color:blue;">in</span> productInventoryGroup.DefaultIfEmpty(<span style="color:blue;">new</span> <span style="color:#2b91af;">Inventory</span>() { ProductId = product.ProductId, Count = 0 })</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">select</span> <span style="color:blue;">new</span></p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name = product.Name,</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ProductId = product.ProductId,</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Count = item.Count</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</p></div> <p>The first thing we do is get a reference to an instance of the class (<em>product</em>) out of the <em>productList</em> with the from clause.&nbsp; We then can join that to the <em>inventoryList.&nbsp; </em>Remember in LINQ, you always have to have a reference to the class itself and not the collection when performing join and where operations, so we also get a reference to an instance of the class (<em>inventory</em>).&nbsp; Once we have that we can use the on clause to relate the two lists using the <em>ProductId </em>field.&nbsp; When doing a join you must use the keyword <em>equals</em> to relate the two collections.</p> <p>Here is where things really differ from what you may be used to.&nbsp; The first thing that is different is that the result of the join is stored in a new variable using the into clause (in this case <em>productInventoryGroup</em>).&nbsp; This is because in LINQ you use the <em>DefaultIfEmpty()</em> method to specify default values to return when there are no matching values on the right side of the join.&nbsp; You can specify no parameter on this method to use the default when it is empty (usually null or 0 for an int), or you can pass a value for it to use.&nbsp; In this case I created a new instance of Inventory and set <em>Count</em> to 0.&nbsp; You then use another from clause to reference an item from that join.&nbsp; Once you get to this point, it is just a matter of creating a new anonymous type, with product.Name and item.Count as values.&nbsp; Remember, item in this case is of type Inventory and will contain the value from that list or the default if there is no match.</p> <p>You can then iterate through the result of the join like usual.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">foreach</span> (<span style="color:blue;">var</span> item <span style="color:blue;">in</span> productInventory)</p> <p style="margin:0px;">{</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&quot;{0}: {1}&lt;br /&gt;&quot;</span>, item.Name, item.Count));</p> <p style="margin:0px;">}</p></div> <p>Which would return the following results.</p><pre>Product 1: 54
Product 2: 0
Product 3: 31
</pre>
<p>It really isn&#39;t that bad, but it&#39;s just different than what you might be used to coming from the T-SQL world.&nbsp; Hopefully, this explanation helps and will be of use to you.&nbsp; </p>
<p>Of course, don&#39;t forget that tomorrow night is the <a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/09/24/announcing-the-first-tulsa-sharepint-meetup.aspx">SharePint meetup</a> at CrawPappy&#39;s at 6:00pm.&nbsp; Thanks again.</p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=743" width="1" height="1">LINQLINQ to SQLC# 3.0Updating Data with LINQ to SQL and GridViews when using a Viewhttp://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/10/07/updating-data-with-linq-to-sql-and-gridviews-when-using-a-view.aspxTue, 07 Oct 2008 18:27:59 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:728CoreyRoth0http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=728http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/10/07/updating-data-with-linq-to-sql-and-gridviews-when-using-a-view.aspx#comments<p>Look! It&#39;s a post that isn&#39;t about SharePoint!&nbsp; Today, I want to talk about some of the issues I have run into when trying to update data using a GridView when bound to a LinqDataSource.&nbsp; This data source has an <em>EnableUpdate</em> property, but unfortunately there is more to it than just that.&nbsp; It works pretty well, but there are a few things that you have to do otherwise you will get the following error.</p> <p><em>The Entity is not of the correct type.</em></p> <p>In today&#39;s example, I required a stored procedure to do the update because I was updating multiple tables behind the scenes.&nbsp; Although LINQ can do this, the complexity of the update was simpler if I did it via stored procedure.&nbsp; The data I am displaying in my GridView is based off a database view.&nbsp; The first thing to do is drag the View into your LINQ to SQL class.&nbsp; You need to make sure your view includes a primary key.&nbsp; You then need to select the column and mark it as a Primary Key in the LINQ to SQL designer.&nbsp; After you have your view defined, drag the stored procedure for your update into the designer.&nbsp; Then on the properties of your view&#39;s data class, you can set a default method for Insert, Update, and Delete.&nbsp; In this case I am only worrying about Update.&nbsp; If you click on Update, it will bring up a <em>Configure Behavior </em>window which will allow give you two choices.&nbsp; <em>Use Runtime </em>will dynamically generate the CRUD logic at runtime.&nbsp; In our case though, we will choose Customize and then pick our stored procedure out of the list.</p> <p>After you have selected an update method, your LINQ to SQL class is good to go.&nbsp; You are now ready to build your page.&nbsp; Start by dragging a GridView and LinqDataSource onto your page.&nbsp; On your LinqDataSource, set the EnableUpdate property to true.&nbsp; Use the wizard to bind your LINQ to SQL database.&nbsp;&nbsp; Pick the Entity named after your view but when configuring columns be sure and choose all columns (*).&nbsp; If you select individual columns, it populates the <em>Select </em>property on the LinqDataSource and you will not be able to make updates.&nbsp; At this point, you can bind your GridView to your LinqDataSource.&nbsp; If you have followed all of the steps so far, the Enable Editing option should be available for you to check.&nbsp; Check it and then the last thing you have to do is set the <em>DataKeyNames</em> property on the GridView.&nbsp; Set it to the name of your primary key and you should be ready to make updates using&nbsp; a GridView.</p> <p>I always find that I spend more time than I care to setting up GridViews.&nbsp; Hopefully this will help if you ever need to use LINQ to SQL with a GridView.&nbsp; Also don&#39;t forget <a href="http://techfests.com/Tulsa/2008/default.aspx">Tulsa TechFest</a> this week where I will be talking about <a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/09/02/speaking-at-tulsa-tech-fest-again.aspx">Code Access Security in SharePoint</a>.</p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=728" width="1" height="1">LINQ to SQLASP.NETC# 3.0Checking for empty EnumerableRowCollections when using LINQhttp://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/04/24/checking-for-empty-enumerablerowcollections-when-using-linq.aspxThu, 24 Apr 2008 15:18:03 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:567CoreyRoth0http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=567http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/04/24/checking-for-empty-enumerablerowcollections-when-using-linq.aspx#comments<p>After you query something with LINQ, you may want to know if any results were returned before your start enumerating or working with the <em>EnumerableRowCollection</em> (that&#39;s most likely the type behind that var you are using in most cases).&nbsp; Consider the following example.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:#2b91af;">DataTable</span> myDataTable = <span style="color:blue;">new</span> <span style="color:#2b91af;">DataTable</span>();</p> <p style="margin:0px;">&nbsp;</p> <p style="margin:0px;"><span style="color:blue;">var</span> queryResults = <span style="color:blue;">from</span> queryResult <span style="color:blue;">in</span> myDataTable.AsEnumerable()</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">where</span> queryResult.Field&lt;<span style="color:#2b91af;">DateTime</span>&gt;(<span style="color:#a31515;">&quot;ItemDateTime&quot;</span>) &lt; <span style="color:#2b91af;">DateTime</span>.Now</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">select</span> <span style="color:blue;">new</span></p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; intColumn1 = queryResult.Field&lt;<span style="color:blue;">int</span>&gt;(<span style="color:#a31515;">&quot;IntColumn1&quot;</span>),</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stringColumn1 = queryResult.Field&lt;<span style="color:blue;">string</span>&gt;(<span style="color:#a31515;">&quot;StringColun1&quot;</span>)</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };</p></div> <p>At first you might try something like this.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">if</span> (queryResults != <span style="color:blue;">null</span>)</p></div> <p>However this will just tell you if the collection is null (and it seems that the collection always has some value regardless of the results of the query).&nbsp; There are two ways that I can think of off the top of my head to check for this.&nbsp; First, you can use the <strong>Count() </strong>method.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">if</span> (queryResults.Count() &gt; 0)</p></div> <p>If you don&#39;t like that, you can also use the more eloquent <strong>Any()</strong> method.&nbsp; I&#39;ve mentioned this method in the past as a way to <a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/06/how-to-check-for-nulls-when-using-linq-to-xml.aspx">check for nulls</a>.&nbsp; Any() can be used to check to see if just about anything exists including elements, attributes, and rows.&nbsp; Here is what it looks like.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">if</span> (queryResults.Any())</p> <p style="margin:0px;">{</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">var</span> result = queryResults.First();</p> <p style="margin:0px;">&nbsp;</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">Console</span>.WriteLine(<span style="color:#a31515;">&quot;Results: {0}&quot;</span>, result.intColumn1);</p> <p style="margin:0px;">}</p></div> <p>The reason I bring this up is that, you have to check if something exists in the collection before calling the <strong>First()</strong> method.&nbsp; Although, you really don&#39;t have to check to see if rows exists before enumerating this collection, you do have to use it before calling First() because it will throw an exception if no rows exist.&nbsp; Btw, First() returns the first item in the EnumerableRowCollection if that wasn&#39;t obvious already.</p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=567" width="1" height="1">LINQC# 3.0Using an Extension Method to remove a Web Part from the Galleryhttp://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/04/15/using-an-extension-method-to-remove-a-web-part-from-the-gallery.aspxTue, 15 Apr 2008 15:00:38 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:561CoreyRoth2http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=561http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/04/15/using-an-extension-method-to-remove-a-web-part-from-the-gallery.aspx#comments<p>When you deactivate a feature that deployed a web part, it is a good idea to remove that web part from the gallery.&nbsp; This makes things much more likely to work when you upgrade said web part later.&nbsp; Like everything in SharePoint, this task is more complicated than it needs to be.&nbsp; You would think you could just call myWeb.WebParts.Delete(&quot;mywebpart.dwp&quot;), but of course that is not the case.&nbsp; That would be too easy.</p> <p>To work with the web parts in the gallery, you first have to know where they are.&nbsp; The Web Part Gallery is just a list, so it can be manipulated just like any other list.&nbsp; To get the list, we use the <strong>GetCatalog</strong> method on the SPWeb object.&nbsp; It takes an enum parameter of <em>SPListTemplateType.WebPartGallery</em>.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:#2b91af;">SPList</span> webPartGallery = currentSite.GetCatalog(<span style="color:#2b91af;">SPListTemplateType</span>.WebPartCatalog);</p></div> <p>Now we need to delete the web part from the gallery.&nbsp;&nbsp; Unfortunately, there is neither an indexer that uses the web part&#39;s name, nor a find method.&nbsp; This means we get to loop through the collection until we find the item we are looking for.&nbsp; This provides for another great place to use an Extension Method.&nbsp; I have found these to be highly useful to make up for the shortcomings in the SharePoint API.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">ExtensionMethods</span></p> <p style="margin:0px;">{</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> Remove(<span style="color:blue;">this</span> <span style="color:#2b91af;">SPList</span> myList, <span style="color:blue;">string</span> key)</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">for</span> (<span style="color:blue;">int</span> i = 0; i &lt; myList.Items.Count; i++)</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">if</span> (<span style="color:blue;">string</span>.Compare(myList.Items[i].Name, key, <span style="color:blue;">true</span>) == 0)</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myList.Items[i].Delete();</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">return</span>;</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; }</p> <p style="margin:0px;">}</p></div> <p>The code is simple.&nbsp; Just loop through the collection and compare the name property to a key parameter on the method.&nbsp; The key parameter will contain the name of the web part&#39;s filename (i.e.: mywebpart.dwp).&nbsp; To use the extension method, it would be called as in the sample below.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">using</span> (<span style="color:#2b91af;">SPWeb</span> currentSite = <span style="color:#2b91af;">SPContext</span>.Current.Web)</p> <p style="margin:0px;">{</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:#2b91af;">SPList</span> webPartGallery = currentSite.GetCatalog(<span style="color:#2b91af;">SPListTemplateType</span>.WebPartCatalog);</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; webPartGallery.Remove(<span style="color:#a31515;">&quot;mywebpart.dwp&quot;</span>);</p> <p style="margin:0px;">}</p></div> <p>This seems like a lot of code to be able to do something that should only take one line of code, but that is what is required.&nbsp; My collection of SharePoint Extension Methods is getting huge.&nbsp; </p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=561" width="1" height="1">SharePointC# 3.0How to: Use LINQ to SQL without using the Object Relational Designerhttp://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/17/how-to-use-linq-to-sql-without-using-the-object-relational-designer.aspxTue, 18 Mar 2008 00:54:20 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:537CoreyRoth2http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=537http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/17/how-to-use-linq-to-sql-without-using-the-object-relational-designer.aspx#comments<p>LINQ to SQL has already proved to be extremely easy to use to create object relational mappings when you have an existing database schema using the Object Relational Designer.&nbsp; This designer is good, but you may not want something that is autogenerating your domain classes.&nbsp; You may want to generate your domain classes yourself.&nbsp; This is actually quite easy and works in a similar manner to other OR/Ms such as ActiveRecord.&nbsp; The thing I like about it is that your domain objects do not have to inherit from some base class that has all of the underlying logic to access the database.&nbsp; Instead you create a custom class separate from your domain objects that inherits from DataContext.</p> <p>We&#39;ll create a simple example of a products table for an e-Commerce web site.&nbsp; Let&#39;s start by looking at the domain object.&nbsp; Before you create your domain object start by adding a reference to <em>System.Data.Linq</em> to your class library if it is not already present.&nbsp; You will then need to add a using statement in each domain class for <em>System.Data.Linq.Mapping</em>.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;">[<span style="color:#2b91af;">Table</span>(Name=<span style="color:#a31515;">&quot;Products&quot;</span>)]</p> <p style="margin:0px;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">Product</span></p> <p style="margin:0px;">{</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">Column</span>]</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">string</span> Name;</p> <p style="margin:0px;">&nbsp;</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">Column</span>(IsPrimaryKey=<span style="color:blue;">true</span>, Name=<span style="color:#a31515;">&quot;Id&quot;</span>)]</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">int</span> ProductId;</p> <p style="margin:0px;">&nbsp;</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; [<span style="color:#2b91af;">Column</span>(Name=<span style="color:#a31515;">&quot;Price&quot;</span>)]</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:blue;">double</span> Price;</p> <p style="margin:0px;">}</p></div> <p>The first thing you do in your domain class is decorate it with a <strong>Table</strong> attribute.&nbsp; An optional parameter here specifies the name of the underlying database table.&nbsp; In this case my domain object it Product but my database table is named Products.&nbsp; I then defined three properties representing columns in the table.&nbsp; The <strong>Column</strong> attribute specifies that the property will have a corresponding column in a database table.&nbsp; The <em>IsPrimaryKey</em> parameter specifies that the column is a primary key in the database.&nbsp; The <em>Name</em> parameter here also allows you to specify a different column name in the database.</p> <p>That is really all that is required to create a domain object.&nbsp; You can define a class for each domain object you want and you can also create relations between them (but I won&#39;t be covering that here today).&nbsp; Once you have your domain object created, you will need to create a DataContext class to actually be able to query your domain objects.&nbsp; This is also pretty simple.&nbsp; You just expose a property with the generic type of Table&lt;&gt; for each one of your domain objects.&nbsp; The name of the property is what you will use with the DataContext when you are querying with LINQ.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">public</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">StoreDataContext</span> : <span style="color:#2b91af;">DataContext</span></p> <p style="margin:0px;">{</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> <span style="color:#2b91af;">Table</span>&lt;<span style="color:#2b91af;">Product</span>&gt; Products;</p> <p style="margin:0px;">&nbsp;</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; <span style="color:blue;">public</span> StoreDataContext(<span style="color:blue;">string</span> connection)</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : <span style="color:blue;">base</span>(connection)</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; {</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp; }</p> <p style="margin:0px;">}</p></div> <p>Now that you have your domain objects written you will need to create the SQL tables that they represent.&nbsp; You can do this manually, or you can have LINQ create the whole database for you.&nbsp; Just create an instance of your DataContext and call the <em>CreateDatabase</em> method.&nbsp; This method infers the name of the database given the connection string you used.&nbsp; If you did not specify the database, you need to add a <strong>Database</strong> attribute with the name to your class.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:#2b91af;">StoreDataContext</span> myDataContext = <span style="color:blue;">new</span> <span style="color:#2b91af;">StoreDataContext</span>(myConnectionString);</p> <p style="margin:0px;">myDataContext.CreateDatabase();</p></div> <p>Alright, so now your domain objects and database are created, now you just need to query something with it.</p> <div style="font-size:10pt;background:white;color:black;font-family:courier new;"> <p style="margin:0px;"><span style="color:blue;">var</span> products = <span style="color:blue;">from</span> product <span style="color:blue;">in</span> myDataContext.Products</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">where</span> product.Price &gt; 49.99f</p> <p style="margin:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">select</span> product;</p></div> <p>This simple query simply returns any product with a price greater than 49.99.&nbsp; So, LINQ to SQL doesn&#39;t have to be completely domain driven.&nbsp; This gives you a lot of flexibility and makes it easy to add additional things to your domain logic if you want to.&nbsp; The downside to this of course it that, when your database schema changes, your domain object is not going to get updated at the click of a button.&nbsp; If you are building your domain objects in this manner though, this is probably not a concern to you though.</p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=537" width="1" height="1">LINQLINQ to SQLASP.NETVisual Studio 2008C# 3.0Using an Extension Method to check if an item exists in a SharePoint collectionhttp://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/03/using-an-extension-method-to-check-if-an-item-exists-in-a-sharepoint-collection.aspxMon, 03 Mar 2008 16:13:00 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:527CoreyRoth2http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=527http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/03/using-an-extension-method-to-check-if-an-item-exists-in-a-sharepoint-collection.aspx#comments<p>As I have mentioned before, one of my biggest complaints about SharePoint is that none of the collections in the SharePoint API have any way to determine if an item exists.&nbsp; Extension Methods offer a slightly more elegant way to do this, although the underlying code still violated multiple best practice rules.&nbsp; Take a look at this example using the SPFileCollection.</p>
<div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;">
<p style="MARGIN:0px;"><span style="COLOR:blue;">public</span> <span style="COLOR:blue;">static</span> <span style="COLOR:blue;">bool</span> Contains(<span style="COLOR:blue;">this</span> <span style="COLOR:#2b91af;">SPFileCollection</span> fileCollection, <span style="COLOR:blue;">string</span> index)</p>
<p style="MARGIN:0px;">{</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">try</span></p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="COLOR:#2b91af;">SPFile</span> testFile = fileCollection[index];</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">return</span> <span style="COLOR:blue;">true</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">catch</span> (<span style="COLOR:#2b91af;">SPException</span> e)</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">return</span> <span style="COLOR:blue;">false</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="MARGIN:0px;">}</p></div>
<p>If you&#39;re not familiar with Extension Methods yet, they are an addition in C# 3.0 that allow you to add methods to existing classes without having to inherit from them.&nbsp; You prefix the first parameter with the keyword <strong>this</strong> followed by a type to specify what type you are extending.&nbsp; You can put your extension method in any class you want.&nbsp; Inside, the method, you see the typical way of checking to see if something exists in a SharePoint collection: try/catch.&nbsp;&nbsp; The syntax for using the extension method is below.</p>
<div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:courier new;">
<p style="MARGIN:0px;"><span style="COLOR:blue;">bool</span> fileExists = fileCollection.Contains(<span style="COLOR:#a31515;">&quot;SomeFile&quot;</span>);</p></div>
<p>Extension methods are quite powerful and I think they can provide an excellent way to make many tasks easier and cleaner inside the SharePoint API.</p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=527" width="1" height="1">SharePointC# 3.0Making DataSets tolerable using LINQ to DataSethttp://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/18/making-datasets-tolerable-using-linq-to-dataset.aspxMon, 18 Feb 2008 16:01:00 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:504CoreyRoth1http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=504http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/18/making-datasets-tolerable-using-linq-to-dataset.aspx#comments<p>Unfortunately, most of us aren&#39;t working in a perfect world, so it is bound to happen that you run into a dataset or two.&nbsp; Whatever the reason (the developer was lazy, you&#39;re maintaining legacy code, someone didn&#39;t know any better, or you&#39;re just working with the SharePoint API), it would be nice if there was a way to make dealing with datasets easier.&nbsp; LINQ to DataSet does this by allowing you to perform queries or move the data easily into a domain object.</p>
<p>To work with LINQ to DataSet, an extension method called <em>AsEnumerable()</em> is tacked onto the DataTable class making it queryable by LINQ This extension method is provided by adding System.Data.DataSetExtensions.dll to your project.&nbsp; Then, it is just a matter of knowing the syntax to get individual columns of data.&nbsp; Using a generic xtension method called <em>Field, </em>we can get the value of a column with the appropriate type.</p>
<p>In this example, we are going to filter the datatable on the ItemDateTime field and return a new anonymous type.&nbsp; We are assuming the datatable contains columns IntColumn1, StringColumn1, and ItemDateTime.</p>
<div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;">
<p style="MARGIN:0px;"><span style="COLOR:blue;">var</span> queryResults = <span style="COLOR:blue;">from</span> queryResult <span style="COLOR:blue;">in</span> myDataTable.AsEnumerable()</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="COLOR:blue;">where</span> queryResult.Field&lt;<span style="COLOR:#2b91af;">DateTime</span>&gt;(<span style="COLOR:#a31515;">&quot;ItemDateTime&quot;</span>) &lt; <span style="COLOR:#2b91af;">DateTime</span>.Now</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="COLOR:blue;">select</span> <span style="COLOR:blue;">new</span></p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; {</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; intColumn1 = queryResult.Field&lt;<span style="COLOR:blue;">int</span>&gt;(<span style="COLOR:#a31515;">&quot;IntColumn1&quot;</span>),</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; stringColumn1 = queryResult.Field&lt;<span style="COLOR:blue;">string</span>&gt;(<span style="COLOR:#a31515;">&quot;StringColun1&quot;</span>)</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp; };</p></div>
<p>This really makes it easy to perform subqueries on things presented to you via datasets.&nbsp; What if you want to get away from that nasty dataset and work with a domain object?&nbsp; LINQ makes that pretty easy as well.&nbsp; Just assign the values into a domain object.&nbsp;&nbsp;Here is a simple domain object (note: that it uses automatic properties).</p>
<div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;">
<p style="MARGIN:0px;"><span style="COLOR:blue;">public</span> <span style="COLOR:blue;">class</span> <span style="COLOR:#2b91af;">MyDomainObject</span></p>
<p style="MARGIN:0px;">{</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">public</span> <span style="COLOR:#2b91af;">DateTime</span> ItemDateTime</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">get</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">set</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="MARGIN:0px;">&nbsp;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">public</span> <span style="COLOR:blue;">int</span> IntColumn1</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">get</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">set</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="MARGIN:0px;">&nbsp;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">public</span> <span style="COLOR:blue;">string</span> StringColumn1</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; {</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">get</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">set</span>;</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; }</p>
<p style="MARGIN:0px;">}</p></div>
<p>Here is how you would assign it to the domain object.</p>
<div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;">
<p style="MARGIN:0px;"><span style="COLOR:blue;">var</span> queryResults2 = <span style="COLOR:blue;">from</span> queryResult <span style="COLOR:blue;">in</span> myDataTable.AsEnumerable()</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="COLOR:blue;">select</span> <span style="COLOR:blue;">new</span> <span style="COLOR:#2b91af;">MyDomainObject</span></p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; {</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ItemDateTime = queryResult.Field&lt;<span style="COLOR:#2b91af;">DateTime</span>&gt;(<span style="COLOR:#a31515;">&quot;ItemDateTime&quot;</span>),</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; IntColumn1 = queryResult.Field&lt;<span style="COLOR:blue;">int</span>&gt;(<span style="COLOR:#a31515;">&quot;IntColumn1&quot;</span>),</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; StringColumn1 = queryResult.Field&lt;<span style="COLOR:blue;">string</span>&gt;(<span style="COLOR:#a31515;">&quot;StringColun1&quot;</span>)</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; };</p></div>
<p>Who knows how efficient this is,&nbsp;but it is quite simple.&nbsp;&nbsp;If you are curious what type queryResults2 is in this, it is an EnumerableRowCollection&lt;MyDomainObject&gt;.&nbsp; If you want it as a list, you can use the ToList() method, there is also ToArray() and ToDictionary() if that&#39;s what you want.&nbsp; That&#39;s all on LINQ and datasets for today.&nbsp; Hopefully, this will make your next experience with them better.</p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=504" width="1" height="1">LINQC# 3.0LINQ to DataSetUsing Object Initalizers yet?http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/14/using-object-initalizers-yet.aspxThu, 14 Feb 2008 15:03:00 GMTceb7fe2a-c56b-4d85-99e6-8dd548580538:497CoreyRoth0http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=497http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/14/using-object-initalizers-yet.aspx#comments<p>Object Initalizers aren&#39;t talked about very much as one of the new features in C# 3.0, but I find them pretty useful from time to time (esepcially when dealing with the SharePoint API).&nbsp; They are great, when there isn&#39;t a constructor that has all of the arguments that you need to initialize the class with in it.&nbsp; Here is an example of how I used it lately with the KeywordQuery class.</p>
<div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;">
<p style="MARGIN:0px;"><span style="COLOR:#2b91af;">KeywordQuery</span> myQuery = <span style="COLOR:blue;">new</span> <span style="COLOR:#2b91af;">KeywordQuery</span>(siteCollection)</p>
<p style="MARGIN:0px;">{</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; QueryText = <span style="COLOR:blue;">string</span>.Format(<span style="COLOR:#a31515;">&quot;Color:\&quot;{0}\&quot;&quot;</span>, myColor),</p>
<p style="MARGIN:0px;">&nbsp;&nbsp;&nbsp; ResultTypes = <span style="COLOR:#2b91af;">ResultType</span>.RelevantResults</p>
<p style="MARGIN:0px;">};</p></div>
<p>In this case I wanted to initialize the QueryText and ResultType properties. This makes the syntax much cleaner.&nbsp; Obviously this feature isn&#39;t life changing, but I find it a nice convenience.</p><img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=497" width="1" height="1">SharePointC# 3.0