Locating people by roleId Basically, I'm trying to find all active people with a roleId of 3 (analyst id). But I want to exclude ids that were created as test analysts, i.e. "130", "252", etc. I see that I'm repeating the part to exclude the test ids. Also, I'm repeating the part about the roleId. Isn't there a way to optimize it? My query works fine, but I'm trying to optimize to make it more DRY.

Here's what my code looks like (I've labeled where I think the code can be optimized):

from people in xDoc.Descendants("People")
where (int?) Convert.ToInt16(people.Element("active").Value) == 1 &amp;&amp;
    (int?)Convert.ToInt16(people.Element("id").Value) != 130 &amp;&amp;  // &lt;-- to optimize
    (int?)Convert.ToInt16(people.Element("id").Value) != 252 &amp;&amp;  // &lt;-- to optimize
    (int?)Convert.ToInt16(people.Element("id").Value) != 264 &amp;&amp;  // &lt;-- to optimize
    (int?)Convert.ToInt16(people.Element("id").Value) != 302 &amp;&amp;  // &lt;-- to optimize
    people.Descendants("roleIds").Descendants("int")
        .Select(p =&gt; (int?)Convert.ToInt16(p.Value)).FirstOrDefault() == 3  // &lt;-- to optimize
select new PeopleClass
{
    ResAnId = (int) Convert.ToInt16(people.Element("id").Value),
    DpInit = (string)people.Element("clientCode").Value,
    ResAnFName = (string)people.Element("firstName").Value,
    ResAnLName = (string)people.Element("lastName").Value,
    ResAnName = String.Format("{0} {1}", (string)people.Element("firstName").Value,
        (string)people.Element("lastName").Value),
    RoleId = people.Descendants("roleIds").Descendants("int")
        .Select(p =&gt; (int?) Convert.ToInt16(p.Value)).FirstOrDefault(),  // &lt;-- to optimize
    DivisionId = (int?) Convert.ToInt16(people.Element("divisionId").Value)
}).ToList();


If you're interested, here's my XML feed:



&lt;People&gt;
  &lt;id&gt;269&lt;/id&gt;
  &lt;dummy&gt;false&lt;/dummy&gt;
  &lt;firstName&gt;Steve&lt;/firstName&gt;
  &lt;lastName&gt;Jobs&lt;/lastName&gt;
  &lt;initial&gt;&lt;/initial&gt;
  &lt;title&gt;CEO&lt;/title&gt;
  &lt;active&gt;1&lt;/active&gt;
  &lt;analystPosition&gt;&lt;/analystPosition&gt;
  &lt;clientCode&gt;SJ&lt;/clientCode&gt;
  &lt;clientCode2&gt;&lt;/clientCode2&gt;
  &lt;divisionId&gt;2&lt;/divisionId&gt;
  &lt;updatedBy&gt;245&lt;/updatedBy&gt;
  &lt;associates&gt;
    &lt;Associate&gt;
      &lt;analystId&gt;269&lt;/analystId&gt;
      &lt;associateId&gt;257&lt;/associateId&gt;
      &lt;rank&gt;0&lt;/rank&gt;
    &lt;/Associate&gt;
  &lt;/associates&gt;
  &lt;roleIds&gt;
    &lt;int&gt;3&lt;/int&gt;
    &lt;int&gt;4&lt;/int&gt;
    &lt;int&gt;13&lt;/int&gt;
  &lt;/roleIds&gt;
&lt;/People&gt;
You can introduce range variables to hold data which you need several times in your query:

var analystRoleId = 3;
var testAnalystIds = new[] { 130, 252, 264, 302 };

var activeAnalysts = 
    from p in xdoc.Descendants("People")
    let id = (int)p.Element("id")
    let isActive = (int)p.Element("active") == 1                        
    let isAnalyst = p.Descendants("roleIds").Elements().Any(e =&gt; (int)e == analystRoleId)
    let firstName = (string)p.Element("firstName")
    let lastName = (string)p.Element("lastName")
    where isActive &amp;&amp; isAnalyst &amp;&amp; !testAnalystIds.Contains(id)
    select new PeopleClass {
        ResAnId = id,
        DpInit = (string)p.Element("clientCode"),
        ResAnFName = firstName,
        ResAnLName = lastName,
        ResAnName = $"{firstName} {lastName}",
        RoleId = analystRoleId,
        DivisionId = (int?)p.Element("divisionId")
    };


Also 


You can use interpolated strings instead of String.Format
I believe id always should present in your file and it will not be null. There is no need to cast this value to nullable
I believe you should check all role ids for verifying whether person is analyst. But you can use FirstOrDeafult as well
You don't need to query for roleId second time - if person matched where clause then it's analyst and you know his role id
You don't need to access Value property of XElement. It's better to use explicit cast operator. Otherwise you can get NullReferenceExceptions
If you can change implementation of PeopleClass then start with renaming it to Person and give more descriptive names to its fields. You can also remove backing storage from ResAndName filed and make it calculated on the fly.