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 &&
(int?)Convert.ToInt16(people.Element("id").Value) != 130 && // <-- to optimize
(int?)Convert.ToInt16(people.Element("id").Value) != 252 && // <-- to optimize
(int?)Convert.ToInt16(people.Element("id").Value) != 264 && // <-- to optimize
(int?)Convert.ToInt16(people.Element("id").Value) != 302 && // <-- to optimize
people.Descendants("roleIds").Descendants("int")
.Select(p => (int?)Convert.ToInt16(p.Value)).FirstOrDefault() == 3 // <-- 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 => (int?) Convert.ToInt16(p.Value)).FirstOrDefault(), // <-- to optimize
DivisionId = (int?) Convert.ToInt16(people.Element("divisionId").Value)
}).ToList();
If you're interested, here's my XML feed:
<People>
<id>269</id>
<dummy>false</dummy>
<firstName>Steve</firstName>
<lastName>Jobs</lastName>
<initial></initial>
<title>CEO</title>
<active>1</active>
<analystPosition></analystPosition>
<clientCode>SJ</clientCode>
<clientCode2></clientCode2>
<divisionId>2</divisionId>
<updatedBy>245</updatedBy>
<associates>
<Associate>
<analystId>269</analystId>
<associateId>257</associateId>
<rank>0</rank>
</Associate>
</associates>
<roleIds>
<int>3</int>
<int>4</int>
<int>13</int>
</roleIds>
</People>