1

I'm new to C# and LINQ. I have the following bit of code to extract from an XML:

var objects = from elem in xml
                select new
                {
                   Obj1 = new Obj1((elem.Element("key1")).Value),
                   Obj2 = new Obj2((elem.Element("key2")).Value)
                };

The objects is an Enumerable. Is there a way that I can get this as a Tuple where in I can access the Obj1 and Obj2 directly without the need to iterate?

3
  • 1
    objects.FirstOrDefault() ? Commented Jan 26, 2015 at 11:37
  • Interesting! What would the First do? Commented Jan 26, 2015 at 11:38
  • What is xml and what is structure of xml you are parsing? How many elements your xml have? Do key1 and key2 exist always? Commented Jan 26, 2015 at 11:41

1 Answer 1

4

You can either use objects.First() or objects.FirstOrDefault(). The difference between this two ways is, that FirstOrDefault will return null if there's no object in the IEnumerable. The call of First() will throw an Exception if there is no object.

Sign up to request clarification or add additional context in comments.

Comments

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.