Solved

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

Posted on 2011-09-19
9
727 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

770 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