Solved

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

Posted on 2011-09-19
9
744 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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
 

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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…
Six Sigma Control Plans

624 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