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