Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 759
  • Last Modified:

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

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
metta0_3
Asked:
metta0_3
  • 4
  • 3
  • 2
2 Solutions
 
wdosanjosCommented:
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
 
metta0_3Author Commented:
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
 
wdosanjosCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
effesCommented:
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
 
metta0_3Author Commented:
- <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
 
effesCommented:
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
 
metta0_3Author Commented:
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
 
effesCommented:
No problem with that. He definitely deserves it.
0
 
metta0_3Author Commented:
Qualiteee!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now