HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpModerate

Convert Foreach Loop into Linq in C#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linqconvertintoloopforeach

Problem

I want to convert these loops into simple loop. I want the code to look neat and short. I saw answers related to LINQ but couldn't make out from that. Are there any other possible ways for this snippet?

```
XmlDocument manifestXmlFile = new XmlDocument();
manifestXmlFile.Load(manifestFileName);

foreach (XmlNode rules in manifestXmlFile.DocumentElement.ChildNodes)
{
foreach (XmlNode ruleNode in rules)
{
foreach (XmlNode childNodeAttributes in ruleNode)
{
foreach (XmlNode childNodeAttrib in childNodeAttributes.ChildNodes)
{
XmlElement ruleElement = (XmlElement)ruleNode;
foreach (XmlNode childNodeConditions in childNodeAttrib.ChildNodes)
{
foreach (XmlNode childNodeCond in childNodeConditions.ChildNodes)
{
if (childNodeCond.Name.ToUpper() == "CONDITION")
{
if (childNodeCond.Attributes["type"].Value.ToUpper() == "HEALTHY")
{
string ruleId = ruleElement.Attributes["ruleid"].Value;
string attributeName = childNodeAttrib.Attributes["name"].Value;
string attributeType = childNodeAttrib.Attributes["type"].Value;
string condTypeValue = childNodeCond.Attributes["type"].Value;
string operatorValue = childNodeCond.Attributes["operator"].Value;
string healthyConditionValue = childNodeCond.FirstChild.InnerText;

var guid = new Guid(ruleId);

//Conversion of enum types
PsmsOperator psmsOperator = (PsmsOperator)Enum.Parse(typeof(PsmsOperator), operatorValue, true);
TypeCode psmsAttributeType = (TypeCode)Enum.Parse(typeof(TypeCode),

Solution

I think you can extract small methods to simplify this code. Also you can try XPath to get specified sub-nodes and attributes.

And, yes, you can simplify this code with LINQ:

var ruleList = from rules in manifestXmlFile.DocumentElement.ChildNodes.Cast()
               from ruleNode in rules
               from childNodeAttributes in ruleNode
               from childNodeAttrib in childNodeAttributes.ChildNodes.Cast()
               from childNodeConditions in childNodeAttrib.ChildNodes.Cast()
               from childNodeCond in childNodeConditions.ChildNodes.Cast()
               where childNodeCond.Name.ToUpper() == "CONDITION"
               where childNodeCond.Attributes["type"].Value.ToUpper() == "HEALTHY"
               select new
               {
                   Id = new Guid(ruleElement.Attributes["ruleid"].Value),
                   AttributeName = childNodeAttrib.Attributes["name"].Value,
                   AttributeType = childNodeAttrib.Attributes["type"].Value,
                   CondTypeValud = condTypeValue = childNodeCond.Attributes["type"].Value,
                   OperatorValue = childNodeCond.Attributes["operator"].Value,
                   HealthyConditionValue = childNodeCond.FirstChild.InnerText,
               };

foreach (var r in ruleList)
{
    var psmsOperator = (PsmsOperator)Enum.Parse(typeof(PsmsOperator), r.OperatorValue, true);
    var psmsAttributeType = (TypeCode)Enum.Parse(typeof(TypeCode), r.AttributeType, true);

    var rule = new Rule(r.Id, r.AttributeName, r.HealthyConditionValue, psmsOperator);

    Rule(attributes, r.Id);
}

Code Snippets

var ruleList = from rules in manifestXmlFile.DocumentElement.ChildNodes.Cast<XmlNode>()
               from ruleNode in rules
               from childNodeAttributes in ruleNode
               from childNodeAttrib in childNodeAttributes.ChildNodes.Cast<XmlNode>()
               from childNodeConditions in childNodeAttrib.ChildNodes.Cast<XmlNode>()
               from childNodeCond in childNodeConditions.ChildNodes.Cast<XmlNode>()
               where childNodeCond.Name.ToUpper() == "CONDITION"
               where childNodeCond.Attributes["type"].Value.ToUpper() == "HEALTHY"
               select new
               {
                   Id = new Guid(ruleElement.Attributes["ruleid"].Value),
                   AttributeName = childNodeAttrib.Attributes["name"].Value,
                   AttributeType = childNodeAttrib.Attributes["type"].Value,
                   CondTypeValud = condTypeValue = childNodeCond.Attributes["type"].Value,
                   OperatorValue = childNodeCond.Attributes["operator"].Value,
                   HealthyConditionValue = childNodeCond.FirstChild.InnerText,
               };

foreach (var r in ruleList)
{
    var psmsOperator = (PsmsOperator)Enum.Parse(typeof(PsmsOperator), r.OperatorValue, true);
    var psmsAttributeType = (TypeCode)Enum.Parse(typeof(TypeCode), r.AttributeType, true);

    var rule = new Rule(r.Id, r.AttributeName, r.HealthyConditionValue, psmsOperator);

    Rule(attributes, r.Id);
}

Context

StackExchange Code Review Q#105336, answer score: 11

Revisions (0)

No revisions yet.