<?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] : LINQ</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx</link><description>Tags: LINQ</description><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 20917.1142)</generator><item><title>A simple way to programmatically create SharePoint security groups</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2010/03/08/a-simple-way-to-programmatically-create-sharepoint-security-groups.aspx</link><pubDate>Mon, 08 Mar 2010 17:12:50 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:2930</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=2930</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2010/03/08/a-simple-way-to-programmatically-create-sharepoint-security-groups.aspx#comments</comments><description>&lt;p&gt;When it comes to SharePoint deployments, I try to automate everything I can.&amp;#160; I don’t like manual steps especially when it comes to setting up security.&amp;#160; A common task when deploying any sites is setting up security in some manner.&amp;#160; Today I am going to cover how to easily store definitions your SharePoint security groups in an XML file.&amp;#160; We’ll use LINQ to XML to make reading the file a breeze, and then we’ll use the SharePoint object model to create the groups and add users (or AD groups).&amp;#160;&amp;#160; I’ve blogged on &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/08/25/how-to-create-a-group-in-sharepoint.aspx"&gt;how to create a group&lt;/a&gt; before, but we’re going to take this a step further by giving you code that you can easily add to a feature receiver or console application.&amp;#160; First let’s take a look at the XML file we’re going to use.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&amp;lt;?&lt;span style="color:#cc7832;"&gt;xml&lt;/span&gt; &lt;span style="color:silver;"&gt;version&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;1.0&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;encoding&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;utf-8&lt;/span&gt;&amp;quot; ?&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;lt;&lt;span style="color:#cc7832;"&gt;Groups&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;My Custom Read Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Owner&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\GroupOwner&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Description&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Readonly Permission Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;PermissionLevel&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Read&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestUser1&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestGroup1&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestGroup2&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;/&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;My Custom Contributors Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Owner&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\GroupOwner&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;Description&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Contributors Permission Group&lt;/span&gt;&amp;quot; &lt;span style="color:silver;"&gt;PermissionLevel&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;Contribute&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;&lt;span style="color:#cc7832;"&gt;User&lt;/span&gt; &lt;span style="color:silver;"&gt;Name&lt;/span&gt;=&amp;quot;&lt;span style="color:#a5c25c;"&gt;SHAREPOINT\TestGroup3&lt;/span&gt;&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160; &amp;lt;/&lt;span style="color:#cc7832;"&gt;Group&lt;/span&gt;&amp;gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;lt;/&lt;span style="color:#cc7832;"&gt;Groups&lt;/span&gt;&amp;gt;&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;In this file, I am defining two SharePoint groups.&amp;#160; One that will have read access and one that will have contribute access.&amp;#160; I store the required information needed by the Add method on the SPGroupCollection object.&amp;#160; I then have one or more User elements with the name of my Active Directory user or group.&amp;#160; I tried to keep my XML schema pretty simple.&amp;#160; You can customize it obviously how you want, you would just have to alter your LINQ queries.&amp;#160; &lt;/p&gt;  &lt;p&gt;Let’s take a look at the code we need to make this happen.&amp;#160; I won’t go into as much detail of the object model since I went into it pretty well on my last post.&amp;#160; We’ll just focus on how we use LINQ to XML to read the information we need and then have it create our groups.&amp;#160; My method is called &lt;em&gt;CreateGroups&lt;/em&gt; and it takes an SPWeb object and a string with the filename of the XML document.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; CreateGroups(&lt;span style="color:#ffc66d;"&gt;SPWeb&lt;/span&gt; currentSite, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; groupsFilename)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// get the xml document from the feature folder&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;XDocument&lt;/span&gt; groupsXml = &lt;span style="color:#ffc66d;"&gt;XDocument&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Load(groupsFilename);&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// create a new anoynmous type with the group data&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; groups = &lt;span style="color:#cc7832;"&gt;from&lt;/span&gt; sharePointGroup &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; groupsXml.Root.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Group&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;select&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Name = sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Owner = sharePointGroup.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Owner&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Owner&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Description = sharePointGroup.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Description&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Description&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Empty,&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; PermissionLevel = sharePointGroup.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;PermissionLevel&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;PermissionLevel&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Users = sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;) : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// iterate through the groups and create the groups&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; sharePointGroup &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; groups)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// only create the group if it does not exist&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;if&lt;/span&gt; (!ContainsGroup(currentSite.SiteGroups, sharePointGroup.Name))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the owner to the web site users&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.EnsureUser(sharePointGroup.Owner);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.SiteGroups.Add(sharePointGroup.Name, currentSite.SiteUsers[sharePointGroup.Owner],&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.SiteUsers[sharePointGroup.Owner], sharePointGroup.Description);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the users to the group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; AddUsersToGroup(sharePointGroup.Name, sharePointGroup.Users, currentSite, sharePointGroup.PermissionLevel);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;This seems like kind of a big method at first, but it’s really not that bad.&amp;#160; To keep things simple, I haven’t included any exception handling code.&amp;#160; We are really just querying the XML document, iterating through each group element inside of it, creating the groups, and then adding the users to the group.&amp;#160; The first line of code just creates an XDocument object.&amp;#160; We then construct a LINQ to XML query.&amp;#160; What we want is to return data from each Group element in the document.&amp;#160; The Add method doesn’t like nulls, so we check for them and use string.Empty if the value does not exist in the file.&amp;#160; The one case where I don’t do this is for the Name of the group.&amp;#160; If that is not present, I would rather the process throw an exception.&amp;#160; As for the Users assigned to the group, I grab all of them and add them to our anonymous type like this.&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;Users = sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;).Any() ? sharePointGroup.Elements(&lt;span style="color:#a5c25c;"&gt;&amp;quot;User&amp;quot;&lt;/span&gt;) : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This gives us an &lt;em&gt;IEnumerable&amp;lt;XElement&amp;gt;&lt;/em&gt; that we can pass to a method later to add each Active Directory user (or group) to the SharePoint group.&amp;#160; Once we execute the query, we iterate through each group element.&amp;#160; The first thing we have to do is make sure that the group does not exist.&amp;#160; Of course there is no way to do that other than using the &lt;a href="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"&gt;try/catch&lt;/a&gt; technique.&amp;#160; I will usually wrap this in an extension method, but for today’s purpose, we’ll just call a method to check.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;bool&lt;/span&gt; ContainsGroup(&lt;span style="color:#ffc66d;"&gt;SPGroupCollection&lt;/span&gt; groupCollection, &lt;span style="color:#cc7832;"&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;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;SPGroup&lt;/span&gt; testGroup = groupCollection[index];&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;return&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;true&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;catch&lt;/span&gt; (&lt;span style="color:#ffc66d;"&gt;SPException&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;return&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;false&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;Lame I know.&amp;#160; I’m so happy there are ways to &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/10/21/checking-to-see-if-a-list-item-exists-in-sharepoint-2010.aspx"&gt;get around&lt;/a&gt; this in SharePoint 2010.&amp;#160; Then this starts to look like code from the previous post.&amp;#160; We call .EnsureUser to make sure the domain account of the group owner is registered with the site.&amp;#160; We then just call the Add method with the Name, Owner, default user, and description.&amp;#160; Again there is more info on the &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/08/25/how-to-create-a-group-in-sharepoint.aspx"&gt;previous post&lt;/a&gt; about that method call.&amp;#160; Assuming the group is created, we can then add the users to the group.&amp;#160; We call a new method &lt;em&gt;AddUsersToGroup&lt;/em&gt; which takes the groupName, the users element, an SPWeb, and the permission level.&amp;#160; &lt;/p&gt;  &lt;p&gt;The first thing we do is query the names of the Active Directory users (or groups).&amp;#160; Here we are just grabbing it from the Name attribute of the User element.&amp;#160; I probably could have condensed this query, but at least it’s easy to read.&amp;#160; We then add each user (or group) from the User elements to the group.&amp;#160; If you are curious about the empty parameters, take a look at the previous post.&amp;#160; If you are going to run into an exception, it’s going to be here.&amp;#160; If the group failed to be created or if the user does not exist (i.e.: you typed it in the XML file wrong), this line will throw an exception.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; AddUsersToGroup(&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; groupName, &lt;span style="color:#6897bb;"&gt;IEnumerable&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#ffc66d;"&gt;XElement&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;&amp;gt; users, &lt;/span&gt;&lt;span style="color:#ffc66d;"&gt;SPWeb&lt;/span&gt; currentSite)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// select the username from the xml document&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; userList = &lt;span style="color:#cc7832;"&gt;from&lt;/span&gt; user &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; users&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;select&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Name = user.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the users to the sharepoint group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; user &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; userList)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.SiteGroups[groupName].AddUser(user.Name, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Empty, user.Name, &lt;/span&gt;&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Empty);&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;Now, we’re almost done.&amp;#160; The last thing we need to do is set the permission level on the group.&amp;#160; This is where we specify whether the group has readonly, contribute, full control, etc access to the site.&amp;#160; Be sure and get the name on the permission level right otherwise you will get an exception.&amp;#160; I’ve also blogged about &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/04/02/how-to-assign-a-permission-level-to-a-sharepoint-group.aspx"&gt;how to assign permission levels&lt;/a&gt; before.&amp;#160; Today’s post is really just a great practical use of putting together the things I have posted on before.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; SetRoleDefinitionBinding(&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; groupName, &lt;span style="color:#ffc66d;"&gt;SPWeb&lt;/span&gt; currentSite, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; permissionLevel)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// add the read role definition to the site group&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;SPRoleAssignment&lt;/span&gt; roleAssignment = &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt; &lt;span style="color:#ffc66d;"&gt;SPRoleAssignment&lt;/span&gt;(currentSite.SiteGroups[groupName]);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; roleAssignment.RoleDefinitionBindings.Add(currentSite.RoleDefinitions[permissionLevel]);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; currentSite.RoleAssignments.Add(roleAssignment);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; currentSite.Update();&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;Effectively you create a new SPRoleAssignment by passing it a SPGroup object.&amp;#160; You then add a binding using the existing RoleDefinitions on the site.&amp;#160; You then add the assignment to the site and of course call .Update() so things get saved.&lt;/p&gt;  &lt;p&gt;That’s really all there is to it.&amp;#160; This is a great use of combining information from my previous posts into something that you can use everyday to set security on your sites.&amp;#160; How you execute this code is up to you.&amp;#160; I’ve used it in a feature receiver and in a console application before.&amp;#160; Setting up security through the UI is very slow and painful.&amp;#160; Once you create it on one server, there is no way to move it to another server and that’s not a lot of fun.&amp;#160; This should help you with that and eliminate those nasty manual steps in your deployment process.&amp;#160; &lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=2930" 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/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category></item><item><title>Checking to see if a list item exists in SharePoint 2010</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/10/21/checking-to-see-if-a-list-item-exists-in-sharepoint-2010.aspx</link><pubDate>Wed, 21 Oct 2009 16:02:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:1012</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=1012</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/10/21/checking-to-see-if-a-list-item-exists-in-sharepoint-2010.aspx#comments</comments><description>&lt;p&gt;This is another topic that is quite dear to most SharePoint developers.&amp;#160; We’ve all been there.&amp;#160; We want to know if a list item exists and the indexer is useless.&amp;#160; It throws an exception should we attempt to access an item that does not exist.&amp;#160; Well as we discovered this week there is a new &lt;em&gt;TryGetList &lt;/em&gt;method that works great to determine if a list exists but there really isn’t anything new with a SPBaseCollection.&amp;#160; We can do this a number of ways include spmetal.exe to generate strongly typed classes, but I was looking for a solution that didn’t require me to generate any classes.&amp;#160; We can now use .OfType&amp;lt;T&amp;gt; to get an IEnumerable&amp;lt;T&amp;gt; that we can use.&amp;#160; Once we have IEnumerable&amp;lt;T&amp;gt; it is quite easy to determine if a list item exists using the .Any() method.&amp;#160; Look at the example below.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;using&lt;/span&gt; (&lt;span style="color:#ffc66d;"&gt;SPSite&lt;/span&gt; siteCollection = &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt; &lt;span style="color:#ffc66d;"&gt;SPSite&lt;/span&gt;(&lt;span style="color:#a5c25c;"&gt;&amp;quot;http://moss-server&amp;quot;&lt;/span&gt;))&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#ffc66d;"&gt;SPList&lt;/span&gt; myCustomList = siteCollection.RootWeb.Lists.TryGetList(&lt;span style="color:#a5c25c;"&gt;&amp;quot;MyCustomList&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// doesn&amp;#39;t throw exception!&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;if&lt;/span&gt; (myCustomList != &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; listEnumeration = myCustomList.Items.OfType&amp;lt;&lt;span style="color:#ffc66d;"&gt;SPListItem&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;&amp;gt;();&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;return&lt;/span&gt; listEnumeration.Any(p =&amp;gt; p.Name == &lt;span style="color:#a5c25c;"&gt;&amp;quot;List Item 1&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;As you can see using the lambda expression p =&amp;gt; p.Name we can do a comparison to see if a list item with that name exists.&amp;#160; This will return us a boolean if the item does in fact exist.&amp;#160; As you can see this isn&amp;#39;t quite as good as a native method that checks for an item that exists, but it does work.&amp;#160; If you want to get creative you can also check on other fields such as Title, Id, or whatever.&amp;#160; Take a look at this sample.&lt;/p&gt;  &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;listEnumeration.Any(p =&amp;gt; p[&lt;span style="color:#a5c25c;"&gt;&amp;quot;Title&amp;quot;&lt;/span&gt;] == &lt;span style="color:#a5c25c;"&gt;&amp;quot;List Item 1&amp;quot;&lt;/span&gt;);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;listEnumeration.Any(p =&amp;gt; p.UniqueId == someGuid);&lt;/p&gt; &lt;/div&gt;   &lt;p&gt;As you can see, LINQ makes it quite easy to determine if an item exists.&amp;#160; This may not be the most efficient way to do it as it is rather brute force.&amp;#160; It may only be arguably better than using a try / catch block, but it certainly looks cleaner.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=1012" 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/LINQ/default.aspx">LINQ</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SharePoint+2010/default.aspx">SharePoint 2010</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SPC09/default.aspx">SPC09</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/SP2010+Beta/default.aspx">SP2010 Beta</category></item><item><title>Linq to XML and Deleting Lists on Feature Deactivation</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/09/24/linq-to-xml-and-deleting-lists-on-feature-deactivation.aspx</link><pubDate>Thu, 24 Sep 2009 20:04:04 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:976</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=976</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/09/24/linq-to-xml-and-deleting-lists-on-feature-deactivation.aspx#comments</comments><description>&lt;p&gt;A few months ago, I discussed how to use LINQ to XML to parse your elements.xml file to &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/02/16/linq-to-xml-and-deleting-files-on-feature-deactivation.aspx"&gt;delete any files&lt;/a&gt; that you may have deployed on feature activation.&amp;#160; Today I have decided to reuse this concept to delete any lists that I have created when I deactivate a feature?&amp;#160; Now you may ask, “why would I want to delete a list when a feature is deactivated?&amp;#160; I’ll lose all of my data in that list!”&amp;#160; My answer is: Yes, of course you will, but sometimes when building a feature to deploy a list(s), you want to delete the list each time before you deploy a new version of it.&amp;#160; During development this is a huge time saver.&amp;#160; Right now, I am working on a feature that deploys four document libraries.&amp;#160; This means I have to manually delete each one.&amp;#160; That is a huge waste of time, but what is nice is that we can reuse the concept above with LINQ to XML and delete lists instead.&amp;#160; &lt;/p&gt;  &lt;p&gt;The code is quite similar.&amp;#160; Have a look.&amp;#160; We first, write some code to get the path to the elements.xml file.&amp;#160; This code snippet assumes, it is always named Elements.xml.&amp;#160; Maybe in the future, I will have it look in the feature.xml file, get all of the ElementManifest definitions and then delete whatever it finds in each file.&amp;#160; For now though, we assume, you can change the path as needed.&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;public&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;override&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; FeatureDeactivating(SPFeatureReceiverProperties properties)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;using&lt;/span&gt; (SPWeb currentSite = (SPWeb)properties.Feature.Parent)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; elementsPath = &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Format(&lt;/span&gt;&lt;span style="color:#a31515;font-weight:normal;"&gt;@&amp;quot;{0}\FEATURES\{1}\Elements.xml&amp;quot;&lt;/span&gt;, SPUtility.GetGenericSetupPath(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Template&amp;quot;&lt;/span&gt;), properties.Definition.DisplayName);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; DeleteLists(currentSite, elementsPath);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;From here, it passes a reference to the current SPWeb and the path to the XML document.&amp;#160; The path to the elements.xml file is used to populate an XDocument which we can then query with LINQ to XML.&lt;/p&gt;   &lt;div style="font-family:consolas;background:black;color:white;font-size:13pt;font-weight:bold;"&gt;   &lt;p style="margin:0px;"&gt;&lt;span style="color:#cc7832;"&gt;private&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;void&lt;/span&gt; DeleteLists(SPWeb currentSite, &lt;span style="color:#cc7832;"&gt;string&lt;/span&gt; elementsPath)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;{&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XDocument elementsXml = XDocument.Load(elementsPath);&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; XNamespace sharePointNamespace = &lt;span style="color:#a5c25c;"&gt;&amp;quot;http://schemas.microsoft.com/sharepoint/&amp;quot;&lt;/span&gt;;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// get each URL to each list&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; listInstances = &lt;span style="color:#cc7832;"&gt;from&lt;/span&gt; module &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; elementsXml.Root.Elements(sharePointNamespace + &lt;span style="color:#a5c25c;"&gt;&amp;quot;ListInstance&amp;quot;&lt;/span&gt;)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;select&lt;/span&gt; &lt;span style="color:#cc7832;"&gt;new&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ListUrl = (module.Attributes(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Url&amp;quot;&lt;/span&gt;).Any()) ? module.Attribute(&lt;span style="color:#a5c25c;"&gt;&amp;quot;Url&amp;quot;&lt;/span&gt;).Value : &lt;span style="color:#cc7832;"&gt;null&lt;/span&gt;,&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; };&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// iterate through each list and delete it&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="color:#cc7832;"&gt;var&lt;/span&gt; listInstance &lt;span style="color:#cc7832;"&gt;in&lt;/span&gt; listInstances)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;try&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SPList currentList = currentSite.GetList(&lt;span style="color:#cc7832;"&gt;string&lt;/span&gt;&lt;span style="font-weight:normal;"&gt;.Format(&lt;/span&gt;&lt;span style="color:#a5c25c;"&gt;&amp;quot;{0}/{1}&amp;quot;&lt;/span&gt;, currentSite.Url, listInstance.ListUrl));&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentList.Delete();&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;catch&lt;/span&gt; (System.IO.&lt;span style="color:#ffc66d;"&gt;FileNotFoundException&lt;/span&gt; e)&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// this exception is thrown if the list does not exist&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:green;"&gt;// update the site&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; currentSite.Update();&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color:#cc7832;"&gt;catch&lt;/span&gt;&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/p&gt;    &lt;p style="margin:0px;"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;    &lt;p style="margin:0px;"&gt;}&lt;/p&gt; &lt;/div&gt;  &lt;p&gt;This code is quite similar to that used in my other post, however instead of looking for Module elements we are looking for &lt;em&gt;ListInstance&lt;/em&gt; elements.&amp;#160; It creates a list of those Urls to each list and iterates through them.&amp;#160; For each list it finds, it deletes the list.&amp;#160;&amp;#160; I’ve written code in the past to manually delete lists, I create by name.&amp;#160; That works fine, but the name of each list has to be maintained.&amp;#160; Now, I just attach this to any feature I am working on and it deletes any list (or lists) that I throw at it.&amp;#160; More than likely this isn’t something you want once you go to production, but it will save you a ton of time during development.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=976" 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/LINQ+to+XML/default.aspx">LINQ to XML</category><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/Feature/default.aspx">Feature</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Enterprise+Library/default.aspx">Enterprise Library</category></item><item><title>Code samples from my short talk about LINQ to XML</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/04/29/code-samples-from-my-short-talk-about-linq-to-xml.aspx</link><pubDate>Wed, 29 Apr 2009 15:02:00 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:895</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=895</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2009/04/29/code-samples-from-my-short-talk-about-linq-to-xml.aspx#comments</comments><description>&lt;p&gt;Today, I had the opportunity to give a short introduction on how to do some basic queries on XML documents with LINQ to XML.&amp;nbsp; In the talk, I talked about how to create anonymous types from your XML document so that you can work with strong types.&amp;nbsp; I also showed some uses some of the classes, methods, and properties, they would use most when working with LINQ to XML.&amp;nbsp; &lt;/p&gt;
&lt;p&gt;I also discussed how LINQ to XML can be used to make working with repeating tables in InfoPath easier.&amp;nbsp; Consider the following XML of an InfoPath form.&amp;nbsp; We have a repeating table called &lt;em&gt;RepeatingTable&lt;/em&gt; and in the table there is element called &lt;em&gt;Item&lt;/em&gt;.&lt;/p&gt;
&lt;div style="FONT-FAMILY:consolas;BACKGROUND:black;COLOR:white;FONT-SIZE:13pt;FONT-WEIGHT:bold;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;lt;?&lt;span style="COLOR:#cc7832;"&gt;xml&lt;/span&gt; &lt;span style="COLOR:silver;"&gt;version&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;1.0&lt;/span&gt;&amp;quot; &lt;span style="COLOR:silver;"&gt;encoding&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;utf-8&lt;/span&gt;&amp;quot;?&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:myFields&lt;/span&gt; &lt;span style="COLOR:silver;"&gt;xmlns:xsi&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;http://www.w3.org/2001/XMLSchema-instance&lt;/span&gt;&amp;quot;&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; &lt;span style="COLOR:silver;"&gt;xmlns:my&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-12-30T15:48:37&lt;/span&gt;&amp;quot;&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; &lt;span style="COLOR:silver;"&gt;xmlns:xd&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;http://schemas.microsoft.com/office/infopath/2003&lt;/span&gt;&amp;quot; &lt;span style="COLOR:silver;"&gt;xml:lang&lt;/span&gt;=&amp;quot;&lt;span style="COLOR:#a5c25c;"&gt;en-US&lt;/span&gt;&amp;quot;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:RepeatingTable&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;Polo Shirt&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;26134&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;19.99&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;Jeans&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Name&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;01823&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Color&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;29.99&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Price&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Item&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp; &amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:RepeatingTable&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp; &amp;lt;&lt;span style="COLOR:#cc7832;"&gt;my:Id&lt;/span&gt;&amp;gt;83123&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:Id&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;lt;/&lt;span style="COLOR:#cc7832;"&gt;my:myFields&lt;/span&gt;&amp;gt;&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;If you needed to work with this repeating table when the InfoPath form was loaded again, you could use code like the following.&amp;nbsp; You could also apply a similar technique to work with this data inside a workflow.&lt;/p&gt;
&lt;div style="FONT-FAMILY:consolas;BACKGROUND:black;COLOR:white;FONT-SIZE:13pt;FONT-WEIGHT:bold;"&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:green;"&gt;// use the MainDataSource to read the XML of the InfoPath Form&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;XDocument infoPathDocument = XDocument.Load(&lt;span style="COLOR:#cc7832;"&gt;new&lt;/span&gt; System.IO.&lt;span style="COLOR:#ffc66d;"&gt;StringReader&lt;/span&gt;(MainDataSource.CreateNavigator().OuterXml));&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:green;"&gt;// required to access my: namespace&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;XNamespace myNamespace = &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-12-30T15:48:37&amp;quot;&lt;/span&gt;;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:green;"&gt;// use descendants node to find any element with the name Item in the tree&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&lt;span style="COLOR:#cc7832;"&gt;var&lt;/span&gt; items = &lt;span style="COLOR:#cc7832;"&gt;from&lt;/span&gt; item &lt;span style="COLOR:#cc7832;"&gt;in&lt;/span&gt; infoPathDocument.Descendants(myNamespace + &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;Item&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; &lt;span style="COLOR:#cc7832;"&gt;select&lt;/span&gt; &lt;span style="COLOR:#cc7832;"&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; {&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; Name = item.Element(myNamespace + &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;Name&amp;quot;&lt;/span&gt;).Value,&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; Color = item.Element(myNamespace + &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;Color&amp;quot;&lt;/span&gt;).Value,&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; Price = item.Element(myNamespace + &lt;span style="COLOR:#a5c25c;"&gt;&amp;quot;Price&amp;quot;&lt;/span&gt;).Value&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;&lt;span style="COLOR:#cc7832;"&gt;foreach&lt;/span&gt; (&lt;span style="COLOR:#cc7832;"&gt;var&lt;/span&gt; item &lt;span style="COLOR:#cc7832;"&gt;in&lt;/span&gt; items)&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:green;"&gt;// do something&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;}&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;In the code above, I use the CreateNavigator method of the MainDataSource as usual when working with an InfoPath form and I use a StringReader to load it into an XDocument.&amp;nbsp; Since InfoPath form’s always use the my namespace it is necessary to declare it using the XNamespace object and use it before referring to any element or attribute in the document.&amp;nbsp; In the example above, I create a new anonymous type with the Name, Color, and Price and then I can use a foreach loop to iterate through the items.&lt;/p&gt;
&lt;p&gt;I have attached the code for the example above as well as the other examples I mentioned in the talk to this post.&amp;nbsp; Please, let me know if you have any questions.&lt;/p&gt;
&lt;p&gt;Follow me on &lt;a href="http://twitter.com/coreyroth"&gt;twitter&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=895" width="1" height="1"&gt;</description><enclosure url="http://www.dotnetmafia.com/blogs/dotnettipoftheday/attachment/895.ashx" length="46670" type="application/x-zip-compressed" /><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+XML/default.aspx">LINQ to XML</category><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/Presentations/default.aspx">Presentations</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/InfoPath/default.aspx">InfoPath</category></item><item><title>String[] in columns returned from KeywordQuery.Execute</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/12/11/string-in-columns-returned-from-keywordquery-execute.aspx</link><pubDate>Thu, 11 Dec 2008 16:17:51 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:802</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=802</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/12/11/string-in-columns-returned-from-keywordquery-execute.aspx#comments</comments><description>&lt;p&gt;I have posted in the past about how to use the &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/19/how-to-use-the-moss-enterprise-search-keywordquery-class.aspx"&gt;KeywordQuery&lt;/a&gt; class to execute an Enterprise Search query.&amp;nbsp; This makes it easy to get a DataTable containing search results.&amp;nbsp; However, one caveat with this is that some columns get returned as a string array (string[]) instead of a string.&amp;nbsp; This can make data binding a pain.&amp;nbsp; If you wanted to bind your DataTable to a GridView, it simply will not display these columns.&amp;nbsp; I used to think it had something to do with it only happening on custom managed properties that you create, but that is not actually the cast.&amp;nbsp; It turns out this is actually related to the post yesterday, because it seems to happen for the same reason.&amp;nbsp; If you have a managed property that has more than one crawled property mapped to it, you get a string[] instead of a string.&amp;nbsp; I am sure there is a good reason for this, but I don&amp;#39;t know what it is.&lt;/p&gt; &lt;p&gt;So how do we deal with this?&amp;nbsp; Option 1 of course is to only map one crawled property to your managed properties.&amp;nbsp; This is probably not going to be an ideal solution.&amp;nbsp; Another option we have is to use LINQ to DataSets to coerce that string[] into something usable.&amp;nbsp; First, we execute the query and load into a DataTable as usual.&amp;nbsp; &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:green;"&gt;// execute the query and put it in a dataTable&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;ResultTableCollection&lt;/span&gt; queryResults = keywordQuery.Execute();&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;ResultTable&lt;/span&gt; queryResultsTable = queryResults[&lt;span style="color:#2b91af;"&gt;ResultType&lt;/span&gt;.RelevantResults];&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:#2b91af;"&gt;DataTable&lt;/span&gt; queryDataTable = &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;queryDataTable.Load(queryResultsTable, &lt;span style="color:#2b91af;"&gt;LoadOption&lt;/span&gt;.OverwriteChanges);&lt;/p&gt;&lt;/div&gt; &lt;p&gt;In this case, I want to bind the builtin Title managed property as well as two of my own managed properties.&amp;nbsp; I use LINQ to DataSets to put these into a new anonymous type.&amp;nbsp; The Field property is generic so you can specify the type that you want.&amp;nbsp; In this case string[].&amp;nbsp; Notice the Title property doesn&amp;#39;t require the use of the string array.&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; formattedResults = &lt;span style="color:blue;"&gt;from&lt;/span&gt; row &lt;span style="color:blue;"&gt;in&lt;/span&gt; queryDataTable.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;&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; Title = row.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Title&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MyManagedProperty1 = row.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;[]&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyManagedProperty1&amp;quot;&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;&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; MyManagedProperty2 = row.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;[]&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;MyManagedProperty2&amp;quot;&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;&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;We then can bind this to a GridView as usual (if that&amp;#39;s what you need it for).&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt; &lt;p style="margin:0px;"&gt;SearchGridView.DataSource = formattedResults;&lt;/p&gt; &lt;p style="margin:0px;"&gt;SearchGridView.DataBind();&lt;/p&gt;&lt;/div&gt; &lt;p&gt;I am sure there is a good reason that these fields get returned as a string[].&amp;nbsp; If you don&amp;#39;t like these options, you can always call the &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/07/17/how-to-query-search-using-the-web-service.aspx"&gt;web service&lt;/a&gt; and get the results as XML.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=802" 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/LINQ/default.aspx">LINQ</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/MOSS/default.aspx">MOSS</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Enterprise+Search/default.aspx">Enterprise Search</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ+to+DataSet/default.aspx">LINQ to DataSet</category></item><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>Select Where In with LINQ</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/07/01/select-where-in-with-linq.aspx</link><pubDate>Tue, 01 Jul 2008 22:14:40 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:625</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=625</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/07/01/select-where-in-with-linq.aspx#comments</comments><description>&lt;p&gt;I can&amp;#39;t talk about SharePoint all the time, so I thought I would talk about how to perform a type of query with LINQ.&amp;nbsp; In T-SQL you might have wrote something like this at one point.&lt;/p&gt; &lt;p&gt;&lt;em&gt;SELECT Title, Id FROM Table1 WHERE Id IN (SELECT Id FROM Table2)&lt;/em&gt;&lt;/p&gt; &lt;p&gt;Basically, I am looking for all rows in Table1 where there is a matching Id in Table2.&amp;nbsp; Effectively I want a contains or exists type comparison between tables or lists.&amp;nbsp; I recently ran into a scenario where I wanted to do this and the syntax wasn&amp;#39;t immediately obvious to me so I thought I would post something on it.&amp;nbsp; Let&amp;#39;s start by defining a simple 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:blue;"&gt;public&lt;/span&gt; &lt;span style="color:blue;"&gt;class&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyClass&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;string&lt;/span&gt; Title&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;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;&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; Id&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;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;&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;We&amp;#39;ll then start by adding some test data to a list of this 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;List&lt;/span&gt;&amp;lt;&lt;span style="color:#2b91af;"&gt;MyClass&lt;/span&gt;&amp;gt; myClassList = &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;MyClass&lt;/span&gt;&amp;gt;();&lt;/p&gt; &lt;p style="margin:0px;"&gt;myClassList.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyClass&lt;/span&gt;() { Title = &lt;span style="color:#a31515;"&gt;&amp;quot;Title 1&amp;quot;&lt;/span&gt;, Id = 1 });&lt;/p&gt; &lt;p style="margin:0px;"&gt;myClassList.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyClass&lt;/span&gt;() { Title = &lt;span style="color:#a31515;"&gt;&amp;quot;Title 2&amp;quot;&lt;/span&gt;, Id = 2 });&lt;/p&gt; &lt;p style="margin:0px;"&gt;myClassList.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyClass&lt;/span&gt;() { Title = &lt;span style="color:#a31515;"&gt;&amp;quot;Title 14&amp;quot;&lt;/span&gt;, Id = 14 });&lt;/p&gt;&lt;/div&gt; &lt;p&gt;In this case I want to compare it to a list of integers to find which items of MyClass match.&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:blue;"&gt;int&lt;/span&gt;&amp;gt; subQueryList = &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:blue;"&gt;int&lt;/span&gt;&amp;gt; { 1, 14, 97, 3, 11 };&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Now, to perform the LINQ query.&amp;nbsp; The key to this kind of query makes use of the contains extension method on the list. &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; filteredList = &lt;span style="color:blue;"&gt;from&lt;/span&gt; myClass &lt;span style="color:blue;"&gt;in&lt;/span&gt; myClassList&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; &lt;span style="color:blue;"&gt;where&lt;/span&gt; subQueryList.Contains(myClass.Id)&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; &lt;span style="color:blue;"&gt;select&lt;/span&gt; myClass;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Enumerating this query would return MyClass objects with an Id of 1 and 14.&amp;nbsp; This works well given a simple list of integers but what if we have two different lists of MyClass?&amp;nbsp; Here is our second list.&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;MyClass&lt;/span&gt;&amp;gt; myClassList2 = &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;MyClass&lt;/span&gt;&amp;gt;();&lt;/p&gt; &lt;p style="margin:0px;"&gt;myClassList2.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyClass&lt;/span&gt;() { Title = &lt;span style="color:#a31515;"&gt;&amp;quot;Title 5&amp;quot;&lt;/span&gt;, Id = 5 });&lt;/p&gt; &lt;p style="margin:0px;"&gt;myClassList2.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyClass&lt;/span&gt;() { Title = &lt;span style="color:#a31515;"&gt;&amp;quot;Title 2&amp;quot;&lt;/span&gt;, Id = 2 });&lt;/p&gt; &lt;p style="margin:0px;"&gt;myClassList2.Add(&lt;span style="color:blue;"&gt;new&lt;/span&gt; &lt;span style="color:#2b91af;"&gt;MyClass&lt;/span&gt;() { Title = &lt;span style="color:#a31515;"&gt;&amp;quot;Title 14&amp;quot;&lt;/span&gt;, Id = 14 });&lt;/p&gt;&lt;/div&gt; &lt;p&gt;One way that comes to mind to handle this is to write a LINQ query to get a list of the Ids for the second list and then query in a similar manner.&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:green;"&gt;// get a list of ids from the other list of classes&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; idList = &lt;span style="color:blue;"&gt;from&lt;/span&gt; myClass &lt;span style="color:blue;"&gt;in&lt;/span&gt; myClassList2&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;select&lt;/span&gt; myClass.Id;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:green;"&gt;// subquery using the idList&lt;/span&gt;&lt;/p&gt; &lt;p style="margin:0px;"&gt;&lt;span style="color:blue;"&gt;var&lt;/span&gt; filteredList2 = &lt;span style="color:blue;"&gt;from&lt;/span&gt; myClass &lt;span style="color:blue;"&gt;in&lt;/span&gt; myClassList&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; idList.Contains(myClass.Id)&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; myClass;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;Enumerating filteredList2 would return MyClass objects with an Id of 2 and 14.&amp;nbsp; Instead of using a subquery to get a list of Ids, what about 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;var&lt;/span&gt; filteredList3 = &lt;span style="color:blue;"&gt;from&lt;/span&gt; myClass &lt;span style="color:blue;"&gt;in&lt;/span&gt; myClassList&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; &lt;span style="color:blue;"&gt;where&lt;/span&gt; myClassList2.Contains(myClass.Id)&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; &lt;span style="color:blue;"&gt;select&lt;/span&gt; myClass;&lt;/p&gt;&lt;/div&gt; &lt;p&gt;This will compile just fine, but as expected it returns no results.&amp;nbsp; Although the myClass in each list with Ids of 2 and 14 have identical values, they are different objects.&amp;nbsp; If you wanted to exert some additional effort, you could get this to work, but I am not going to cover that today.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=625" width="1" height="1"&gt;</description><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</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>How to: Use the Group Clause in LINQ</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/26/how-to-use-the-group-clause-in-linq.aspx</link><pubDate>Wed, 26 Mar 2008 14:29:37 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:545</guid><dc:creator>CoreyRoth</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=545</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/26/how-to-use-the-group-clause-in-linq.aspx#comments</comments><description>&lt;p&gt;As you know, LINQ is similar in a lot of ways to T-SQL, but as you go to start doing things with grouping or joining, you will find that that there are some syntactical differences.&amp;nbsp; There are two ways you can use the group clause in LINQ, which way you will use it will be based upon your particular needs.&amp;nbsp; I&amp;#39;ll start by explaining the way I typically use it.&amp;nbsp; In this scenario, I want to group something by a particular column and then also get a count of how many rows have each value.&amp;nbsp; Basically, we are creating the equivalent of the following T-SQL statement.&lt;/p&gt; &lt;p&gt;SELECT ProductName, COUNT(*) AS ProductCount FROM MyTable GROUP BY ProductName&lt;/p&gt; &lt;p&gt;When using the group clause it has to be the last statement in the query unless you use the into keyword.&amp;nbsp; Basically, it replaces the select statement.&amp;nbsp; I&amp;#39;ll talk about that in a minute.&amp;nbsp; To support a scenario more similar to the T-SQL syntax, you make use of the into keyword.&amp;nbsp; Here is what the LINQ query would look like.&amp;nbsp; In this case we are using LINQ to DataSet as the source.&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; productGroups = &lt;span style="color:blue;"&gt;from&lt;/span&gt; row &lt;span style="color:blue;"&gt;in&lt;/span&gt; myDateTable.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;&amp;nbsp; &lt;span style="color:blue;"&gt;group&lt;/span&gt; row &lt;span style="color:blue;"&gt;by&lt;/span&gt; row.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;ProductName&amp;quot;&lt;/span&gt;) &lt;span style="color:blue;"&gt;into&lt;/span&gt; rowGroup&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; &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; {&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; Name = rowGroup.Key,&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; Count = rowGroup.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; };&lt;/p&gt;&lt;/div&gt; &lt;p&gt;The syntax of the group clause specifies what to group (row), how to group it (row.Field&amp;lt;string&amp;gt;(&amp;quot;ProductName&amp;quot;)) and where to put that grouping (rowGroup) so that you can use it.&amp;nbsp; The difference between this and T-SQL is the into clause which is required in order for you to do anything with the grouping in an anonymous type.&amp;nbsp; The variable rowGroup is actually of type of IGrouping&amp;lt;string, DataRow&amp;gt; with string being the type we grouped on.&amp;nbsp; Once the data is grouped into rowGroup, this variable can be used to store the name and count in a new anonymous type.&amp;nbsp; The &lt;em&gt;Key&lt;/em&gt; property contains the value of what we are grouping on and the &lt;em&gt;Count() &lt;/em&gt;method gives us our count.&amp;nbsp; So far when I have grouped in LINQ, I have found this to be the most common scenarios.&amp;nbsp; Typically I want an IEnumerable&amp;lt;&amp;gt; of some sort of anonymous type that I can iterate over or bind to.&lt;/p&gt; &lt;p&gt;If you don&amp;#39;t want an anonymous type at this point, you can use the other use of the group clause without the into keyword.&amp;nbsp; In that case the result of the LINQ query would return the IGrouping&amp;lt;string, dataRow&amp;gt;.&amp;nbsp; You can then either query it again with LINQ or use a nested loop to work with the data.&amp;nbsp; &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; productGroups = &lt;span style="color:blue;"&gt;from&lt;/span&gt; row &lt;span style="color:blue;"&gt;in&lt;/span&gt; myDateTable.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;&amp;nbsp; &lt;span style="color:blue;"&gt;group&lt;/span&gt; row &lt;span style="color:blue;"&gt;by&lt;/span&gt; row.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;ProductName&amp;quot;&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;foreach&lt;/span&gt; (&lt;span style="color:#2b91af;"&gt;IGrouping&lt;/span&gt;&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;DataRow&lt;/span&gt;&amp;gt; productGroup &lt;span style="color:blue;"&gt;in&lt;/span&gt; productGroups)&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;foreach&lt;/span&gt; (&lt;span style="color:blue;"&gt;var&lt;/span&gt; row &lt;span style="color:blue;"&gt;in&lt;/span&gt; productGroup)&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:green;"&gt;// do something with dataRow here&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;I don&amp;#39;t find this scenario nearly as usable but some may find it better.&amp;nbsp; Hopefully this will help the next time you got to group something with LINQ.&amp;nbsp; It&amp;#39;s not really that complicated but I thought it would be worth it to point some of the differences.&amp;nbsp; I&amp;#39;ll probably cover left outer joins pretty soon, because I have done it a few times now and the syntax still gets me tripped up every time.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=545" 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+DataSet/default.aspx">LINQ to DataSet</category></item><item><title>Using LINQ to make Enterprise Search Results more usable</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/25/using-linq-to-make-enterprise-search-results-more-usable.aspx</link><pubDate>Tue, 25 Mar 2008 14:58:37 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:544</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=544</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/03/25/using-linq-to-make-enterprise-search-results-more-usable.aspx#comments</comments><description>&lt;p&gt;In my previous post about using the &lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/19/how-to-use-the-moss-enterprise-search-keywordquery-class.aspx"&gt;KeywordQuery&lt;/a&gt; class, I had one small omission.&amp;nbsp; The fact is that when you add your own managed properties using the SelectProperties collection of the keyword class, the data type you get back is a string[] containing 1 element instead of a string.&amp;nbsp; This can be quite annoying when you are attempting to do data binding as well as additional filtering.&amp;nbsp; I have found this to really only be the case when dealing with custom managed properties.&amp;nbsp; Default properties such as title and content source always return a string.&amp;nbsp; To combat this use LINQ to read the data into a new anonymous type.&amp;nbsp; You can then bind the data and filter as needed.&lt;/p&gt; &lt;div style="font-size:10pt;background:white;color:black;font-family:courier new;"&gt;&lt;span style="color:blue;"&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; results = &lt;span style="color:blue;"&gt;from&lt;/span&gt; queryResult &lt;span style="color:blue;"&gt;in&lt;/span&gt; queryDataTable.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; &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; {&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; Title = queryResult.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;ContentSource&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; Size = (queryResult.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;[]&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Size&amp;quot;&lt;/span&gt;).Any()) ? queryResult.Field&amp;lt;&lt;span style="color:blue;"&gt;string&lt;/span&gt;[]&amp;gt;(&lt;span style="color:#a31515;"&gt;&amp;quot;Size&amp;quot;&lt;/span&gt;)[0] : &lt;span style="color:blue;"&gt;null&lt;/span&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; &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; };&lt;/p&gt;&lt;/div&gt;&lt;/span&gt;&lt;/div&gt; &lt;p&gt;In the above example, I specify ContentSource as a string just like normal.&amp;nbsp; However, for my custom manager property Size, I have to cast to a string[] and then I simply return the first element.&amp;nbsp; Technically, I should probably check to make sure that the element exists as well (but I am checking for &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;null&lt;/a&gt;).&amp;nbsp; Once everything is copied into the anonymous type, you can bind, group, or filter it as needed.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=544" 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/LINQ/default.aspx">LINQ</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/MOSS/default.aspx">MOSS</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Enterprise+Search/default.aspx">Enterprise Search</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>LINQ to SQL Logging</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/21/linq-to-sql-logging.aspx</link><pubDate>Thu, 21 Feb 2008 15:24:41 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:508</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=508</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/02/21/linq-to-sql-logging.aspx#comments</comments><description>&lt;p&gt;The other day I was reading Charlie Calvert&amp;#39;s excellent post on &lt;a href="http://blogs.msdn.com/charlie/archive/2007/12/09/deferred-execution.aspx"&gt;LINQ and Deferred Execution&lt;/a&gt; and stumbled upon a logging feature inside LINQ to SQL.&amp;nbsp; By setting the Log property to a TextWriter (in this case Console.Out), LINQ to SQL will log the query and what parameters it sent to the database server.&amp;nbsp; Assign it before you execute your query like this.&lt;/p&gt;&lt;pre&gt;&lt;span style="mso-ansi-language:en;"&gt;&lt;font size="2"&gt;MyDataContext.Log = &lt;span style="color:#2b91af;"&gt;Console&lt;/span&gt;.Out;&lt;/font&gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;Obviously you don&amp;#39;t have to log to the console, you can log to any text writer.&amp;nbsp; This provides a great way to log what queries are going to the database and helps eliminate the mystery of what queries have been executing in your application.&amp;nbsp; Obviously, you can view these queries in the debugger, but once your application is in production, this is a good solution.&lt;/p&gt;&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=508" 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></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>How I used LINQ to XML to Create Document Library Folders</title><link>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/01/17/how-i-used-linq-to-xml-to-create-document-library-folders.aspx</link><pubDate>Thu, 17 Jan 2008 23:29:24 GMT</pubDate><guid isPermaLink="false">ceb7fe2a-c56b-4d85-99e6-8dd548580538:13</guid><dc:creator>C-Dog's .NET Tip of the Day</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.dotnetmafia.com/blogs/dotnettipoftheday/rsscomments.aspx?PostID=13</wfw:commentRss><comments>http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/01/17/how-i-used-linq-to-xml-to-create-document-library-folders.aspx#comments</comments><description>I have been doing a lot of document library work lately and in my work I had a need to create a series of subfolders in the document library using a particular custom content type. This meant I was going to need to write a little code. I decided to keep...(&lt;a href="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2008/01/17/how-i-used-linq-to-xml-to-create-document-library-folders.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://www.dotnetmafia.com/aggbug.aspx?PostID=13" 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/LINQ+to+XML/default.aspx">LINQ to XML</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/Document+Library/default.aspx">Document Library</category><category domain="http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/tags/LINQ/default.aspx">LINQ</category></item></channel></rss>