5

I am new to C# and taking my first course at a University. I think this is an issue with instantiation but here goes with what I am after.

I am wanting to get the value from te.ED w/o having to go through the multiple for each loops, as if the answer is "No" then there is no need to go through the loops and extract multiple data elements (not being shown). Is there a way to check that value BEFORE going through all of the for each loops?

Code is here

TR reply = service.track(request);
foreach (CTD ctd in reply.CTD)
{
    foreach (TD td in ctd.TD)
    {
        if (td.Events != null)
        {
            foreach (TE te in td.Events)
            {
                if (te.TimestampSpecified)
                {
                    //This is where the field I am after exists
                    if (te.ED == "YES")
                        Console.WriteLine("The answer is yes");
                    else 
                        Console.WriteLine("The answer is no");
                }
            }
        }
    }
}

Per the comment from @Anis Programmer - I believe you are wanting to see the TD element from the class CTD. If that is the case - see below

[System.Xml.Serialization.XmlElementAttribute("TD")]
public TD[] TD {
    get { return this.tdf; }
    set { this.tdf = value; }
}

Per the answer from @Neel below - I am very close with the syntax

var result = reply.CTD.SelectMany(c1 => c1.TD)
                  .SelectMany(c2 => c2.Events.Select(c3 => c3.TimestampSpecified));


foreach (var ltr in result)
    Console.WriteLine(ltr)

Now the issue is that the foreach loop makes two passes, and the value returned from both is True

What do I need to change in this syntax?

6
  • You do not need to abbreviate you variable names so much! Especially when learning use full words that describe what they are for. It will make it easier for you and anyone who reads your code in the future to decipher what is happening. Commented Jan 22, 2017 at 3:33
  • 1
    good idea. I was thinking the less I type out the more efficient it would be. However, as you stated if no one can follow what is going on, or decipher it, it costs more in the long run. Commented Jan 22, 2017 at 3:35
  • Very unclear what you are trying to achieve - you've posted some code (which is good start), but did not explain what you want that code to do (rather than how it does that). Also check out minimal reproducible example guidance on posting code - following it would have helped everyone to see what types are used in the code. Possibly Enumerable.First is the answer, but hard to say without information on what code is expected to do. Commented Jan 22, 2017 at 3:58
  • @AlexeiLevenkov - I want to check the value of te.ED WITHOUT having to use all of the for loops to get there. Does that help clarify? Commented Jan 22, 2017 at 4:02
  • Could you please show us the values of reply.CTD Commented Jan 22, 2017 at 4:13

2 Answers 2

1

I think you can use a LinQ in a foreach like this:

foreach (
    var te in from ctd in reply.CTD
    from td in ctd.TD
    where td.Events != null
    from te in td.Events
    where te.TimestampSpecified
    select te)
{
    Console.WriteLine(te.ED == "YES" ? "The answer is yes" : "The answer is no");
}
Sign up to request clarification or add additional context in comments.

1 Comment

That got it! The only tweak I had to make was to change the syntax to reply.CTD
1

What I assumed from the example you have posted that you want to avoid multiple nested foreach loop.

You can use linq to shorten the same.Here is how using lamda expression.

var result = reply
    .SelectMany(c1 => c1.CTD)
    .SelectMany(c2 => c2.TD)
    .SelectMany(c3 => c3.Events.Select(c4 => c4.TimestampSpecified));

Now you just loop on the result and compare with ED value.

6 Comments

I would just need to loop like such? --- if (timestampspec.ED == "Yes")
result would be a collection of Events with TimestampSpecified property.
If I type var result = reply intellisense does not show SelectMany as an option
Yes, I have using System.Linq in my project.
I figured it out. I have to use reply.CTD.SelectMany and place this under the first foreach
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.