joesan
joesan

Reputation: 15345

C# LINQ to XML Object Extraction

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?

Upvotes: 1

Views: 68

Answers (1)

Tomtom
Tomtom

Reputation: 9384

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.

Upvotes: 4

Related Questions