score:12
well to start with, i don't think you wanted the second descendant overall - you wanted the second chapter, which isn't necessarily the same thing. fortunately it's easy to do:
// use 1 for the second chapter, 2 for "element 2" (i.e. the third element) etc
var chapter = book.descendants("chapter").elementat(1);
var verses = from verse in chapter...
edit: for the verse number, use the overload of select
which provides the element index:
var verses = chapter.element("verse")
.select((element, index) => new {
versenumber = index + 1,
text = element.value
});
i don't think it makes much sense for every verse to have the first and last verse in the paragraph, personally. it's not really a feature of that verse, is it?
score:0
by using the extension method notation you can use the select()
call that will provide you the index of the current element like this:
var indexedchapters = book.decendants().select((chapter, index) => new { chapter, index });
afterwards you can than access the index of the anonymous type in any following linq statement.
score:2
another correct answer, although jon skeet's answer is way more efficient, is this code:
chapter.elementsbeforeself().count()
Source: stackoverflow.com
Related Query
- How to get the value of an XML element using Linq even when empty
- How to get the position (index in order) of an XML element using LINQ?
- How to get all elements except the n'th element in a List using Linq
- How to find the index of next matching element in list using LINQ
- How do I add a new element to the existing XML file on Windows Phone 7 using LINQ to XML?
- How to get the index of int array when using linq query
- How to Get The Values Printed from XML using LINQ or XPath
- How to get the Inner Element value from Elements in xml linq
- How to query XML with the same element and attribute name using linq
- How to get child element value in XML using LINQ
- How to get the name of elements/attributes in xml files and write these into another new xml file using LINQ
- How to get a count of every single element in an xml file using linq to xml?
- Using LINQ to XML how can I group element on the basis of its value?
- How query xml element using linq whether or not the element is existed C#
- How to get the first descendant of a node without knowing the name from XML using LINQ
- How to get the value of elements using XML and Linq based off a specific tag name
- How to get same element value from xml which having same and different nodes using linq in c#
- How to get xml log elements by searching the specific string/text using LINQ
- How to get the index of an element in an IEnumerable?
- Get List<> element position in c# using LINQ
- How do I get the index of the highest value in an array using LINQ?
- How to get elements by name in XML using LINQ
- How do I get the second element in a collection, using LINQ?
- How can I get LINQ to return the index of the object which has the max value in a collection?
- How to check if XML contains element when using LINQ to XML?
- Get array index values of the top 1000 largest entries inside an array using LINQ
- How do I clone a xml element using Linq to Xml
- How can I write the following code more elegantly using LINQ query syntax?
- How to use expressions to build a LINQ query dynamically when using an interface to get the column name?
- How to get the key when using linq Max() method
More Query from same tag
- C# Linq Subset Data
- The right way of returning multiple values from a method
- Asp.net MVC4: Custom paging
- Refactoring LINQ to Query Expressions
- How to get index using LINQ?
- Use of LINQ and ArrayList
- Select template
- Entity Framework - Querying from ObjectContext vs Querying from Navigation Property
- Map Hierarchical Collection of Domain Objects to Hierarchical Collection of ViewModels
- Linq query to filter id inside a list of list c#
- List queries 20 times faster than IQueryable?
- Mapping from join - The type 'CarDTO' appears in two structurally incompatible initializations within a single LINQ to Entities query
- NHibernate nullability check inside where does not work
- Cannot Implicitly convert anonymoustype#1 to Object
- SingleOrDefault() when the sequence contains the default value
- Mapping View to Entity using EF 5 Code First
- how to combine two fields in linq?
- Linq to entities in asp.net and generic delete method
- How linq-to-nhibernate between 2 lists by StartsWith
- groupping/sorting/splitting 2D list/array
- How to translate the following SQL to Linq?
- How can I do a "one hit" Linq query with nullable values?
- C#: how do you check lists are the same size & same elements?
- LINQ/lambda: How can I query a DB table based on information from another table? (many to many relationship)
- query using function “SUBSTR” of Firebird
- Get Sum of a field in Linq with Group by
- System.String[] Split(Char[])' method unrecognized
- How to calculate sum of amount for each 5 weeks before in linq?
- How to code a Foreach Loop for Endless Categories Tree in MVC.Net?
- SQL Server Vs MongoDB : Speed test?