Solved

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

Posted on 2011-09-19
9
722 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
ID: 36562140
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
ID: 36562482
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
ID: 36562554
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
ID: 36562636
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:metta0_3
ID: 36562829
- <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
ID: 36562965
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
ID: 36563387
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
ID: 36563612
No problem with that. He definitely deserves it.
0
 

Author Closing Comment

by:metta0_3
ID: 36563676
Qualiteee!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

911 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

19 Experts available now in Live!

Get 1:1 Help Now