I have an XML file with the following structure:
<Entities> <Request> <ID> A1 </ID> <Finance> <StartDate> Some Date </StartDate> </Finance> <Request> <Request> ... </Request> </Entities>
There may be several queries with the same identifier, but in such cases, the StartDate should be different for each query.
I need to print the last two dates on an identifier.
If it was an SQL table with ID and StartDate columns, I would use the following query, which works fine:
SELECT ID, StartDate FROM ( SELECT ID, StartDate, RANK() OVER (PARTITION BY ID ORDER BY StartDate DESC) rank FROM Request ) WHERE rank IN ('1','2')
But I have the data in XML format, and the best I could come up with was to arrange the data according to ID, StartDate. I still need to dig up the last two dates for each identifier.
var cafrequests =
from request in xdoc.Descendants("Request") orderby (int)request.Element("ID"), (DateTime)request.Element("Finance").Element("StartDate") ascending select new { ID = request.Element("ID"), StartDate = request.Element("Finance").Element("StartDate"), };
Using Take (2), you will only get the top two rows of data, not the top 2 by identifier.
So can anyone tell me what is equivalent to the above SQL statement in LINQ? I don't want to parse and manipulate XML with loops and conditional expressions in C #, and I'm pretty new to LINQ (read about it yesterday and started using it), and I'm still looking at the documentation.