Solved

LINQ to XML, getting XAttribute into a distinct dateTime object using just date

Posted on 2011-09-19
9
718 Views
Last Modified: 2012-05-12
Hi,

I have the following LINQ-to-XML code. I can get this placed into the myAnon variable. What I want to do is get just the date, into an array of dateTime object, I also want to make this datetime object distinct. So just one copy of each individual date, as there can be many with the same data. Using distinct in the below code doesn't work as time interferes, because my startDateTime value includes to complete date time object.

DateTime myDateTime[];

var myAnon= (from e in pData.Descendants("myData")
                          select new
                          {
                              startDateTime = Convert.ToDateTime(e.Attribute("startDateTime").Value)
                          }).OrderBy(p => (DateTime)Convert.ToDateTime(p.startDateTime));

Open in new window



Any useful advice would be gratefully appreciated.

Thanks in advance.
0
Comment
Question by:metta0_3
  • 4
  • 3
  • 2
9 Comments
 
LVL 23

Expert Comment

by:wdosanjos
Comment Utility
Please try the following:

DateTime[] myDateTime;

myDateTime = (from e in pData.Descendants("myData")
	      select Convert.ToDateTime(e.Attribute("startDateTime")))
	     .Distinct()
	     .OrderBy( dt => dt )
	     .ToArray();

Open in new window

0
 

Author Comment

by:metta0_3
Comment Utility
No above doesn't work at all. Already tried. In fact the distinct doesn't really matter I can do that later and the orderby, all I want to do is get the result into a dateTime array for now.

So I just really need to convert part of an anonymous type to DatetTime object. Bit of a nightmare this.
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 200 total points
Comment Utility
Please elaborate on "doesn't work at all".  

- Does it throw a compilation error? (What error?)

- Or, the array does not contain the expected values?  (What values are expected and what do you get?)


Can you provide a sample XML and the expected result?

Thanks
0
 
LVL 6

Expert Comment

by:effes
Comment Utility
Please try changing line 4 of wdosanjos code to
select Convert.ToDateTime(e.Attribute("startDateTime")).Date)

Open in new window

That should set the time-part of the DateTime objects to 00:00:00.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:metta0_3
Comment Utility
- <root id="10014" >
	  <myData startDateTime="04/12/2002 13:10:16" /> 
	  <myData startDateTime="04/12/2002 14:55:43" /> 
	  <myData startDateTime="09/05/2007 09:48:54" /> 
	  <myData startDateTime="12/05/2007 13:16:57" /> 
	  <myData startDateTime="24/11/2007 11:08:38" /> 
	  <myData startDateTime="24/11/2007 12:31:35" /> 
	  <myData startDateTime="24/11/2007 16:51:36" /> 
	  <myData startDateTime="24/11/2007 17:04:55" /> 
	  <myData startDateTime="24/11/2007 17:05:28" /> 
	  <myData startDateTime="24/11/2007 17:17:09" /> 
	  <myData startDateTime="24/11/2007 18:03:17" /> 
	  <myData startDateTime="24/11/2007 20:00:09" /> 
	  <myData startDateTime="22/07/2011 09:51:32" /> 
	  <myData startDateTime="22/07/2011 10:38:14" /> 
  </root>

Open in new window

 
  Ok, above is sample xml, I want to get all distinct dateTimes into a dateTime object. I have a work around, it is just not
  what I want I want to try to do it using LINQ to XML.

  var values = (from e in pMyData.Descendants("myData")
                            select new
                            {
                                startDateTime = Convert.ToDateTime(e.Attribute("startDateTime").Value)
                          }).Distinct().OrderBy(p => (DateTime)Convert.ToDateTime(p.startDateTime));

Open in new window


The above code is as close as I can get. The problem is it clearly doesn't work because of the time, If it did not have the time just date
then it will filter out any repeating dates giving me the desired result like below.
  <root>
	  <myData startDateTime="04/12/2002 /> 
	  <myData startDateTime="09/05/2007 /> 
	  <myData startDateTime="12/05/2007 /> 
	  <myData startDateTime="24/11/2007 />  
	  <myData startDateTime="22/07/2011 /> 
  </root>
 

Open in new window

 
  However, for the purposes of what I am doing I dont want to have an extra attribute in my xml structure just for startDate. Therefore I want to be able to do it on when executing the LINQ-to-XML query.
 
 What effes has proposed is similar to what I have been trying to achieve.
 
  DateTime[] myDateTime;
              myDateTime = (from e in pReportData.Descendants("cdrRecord")
                            select Convert.ToDateTime(e.Attribute("startDateTime")).Date)
                       .Distinct()
                       .OrderBy(dt => dt)
                     .ToArray();
 
 
I am getting the below compilation error when running the above code.
 
Unable to cast object of type 'System.Xml.Linq.XAttribute' to type 'System.IConvertible'.

I'm close I just can't do it.
0
 
LVL 6

Accepted Solution

by:
effes earned 300 total points
Comment Utility
Try
DateTime[] myDateTime;

myDateTime = (from d in pData.Descendants("myData")
             select Convert.ToDateTime(d.Attribute("startDateTime").Value).Date)
             .Distinct()
             .OrderBy(dt => dt)
             .ToArray();

Open in new window

You have to use the Value of the Attribute (line 4 again). Missed that one, sorry.
0
 

Author Comment

by:metta0_3
Comment Utility
effes. Thats it. U did it thanks very much. I knew it could be done. So now onto the points. I will split, unless u have problem with this effes, because wdosanjos made a significant contribution.

Regards,
0
 
LVL 6

Expert Comment

by:effes
Comment Utility
No problem with that. He definitely deserves it.
0
 

Author Closing Comment

by:metta0_3
Comment Utility
Qualiteee!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now