Question

I want to populate a listBox with prayertimes for today, tomorrow and the day after. the XML has values for day and month, not year. The query works without a problem unless tomorrow or the day after tomorrow is in the next year. Then the list shows the values for 01.01 first and then 02.01 and last 31.12.

Question: 1) how to sort it so it sort ascending according to Date. 2)how to fix "Date" so the value of this is shown as monday 01.01.2012 and not "01.01.2012 12:00AM" 2) how to show 01.01 and 02.01 as 2013 and not 2012 because the year changes.

This shows like this in my listbox:

01.01.2012 12:00AM
Fajr 07:00
Soloppgang 09:19
etc..

02.01.2012 12:00AM
Fajr 07:00
Soloppgang 09:19
etc..

31.12.2012 12:00AM
Fajr 07:00
Soloppgang 09:19
etc...

The result i want is achieved the entire year NOT when the nextDay or thirdDay is in the next year.

My XML:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <WIMPrayerTable>
        <Prayertime
            Day ="1" 
            Month="1" 
            Fajr="07:00" 
            Soloppgang="09:19" 
            Zohr="12:26" 
            Asr="13:36"
            Maghrib="15:26" 
        />

        <Prayertime
            Day ="2" 
            Month="1" 
            Fajr="07:00" 
            Soloppgang="09:19" 
            Zohr="12:25" 
            Asr="13:33"
            Maghrib="15:23" 
            Isha="17:39" 
        />

//the rest of the year goes in between here. i am not showing it here

my class:

public class Bonn3
{

    public string Fajr { get; set; }
    public string Soloppgang { get; set; }
    public string Zohr { get; set; }
    public string Asr { get; set; }
    public string Maghrib { get; set; }
    public string Isha { get; set; }
    public string Jumma { get; set; }
    public DateTime Date { get; set; }
  }

my query:

DateTime myDay = DateTime.Now;
DateTime NextDay = myDay.AddDays(1);
DateTime thirdDay = myDay.AddDays(2);


var filteredData = from c in loadedCustomData.Descendants("Bønnetid")
   where c.Attribute("Dag").Value == myDay.Day.ToString() && c.Attribute("Måned").Value == myDay.Month.ToString()
|| c.Attribute("Dag").Value == NextDay.Day.ToString() && c.Attribute("Måned").Value == NextDay.Month.ToString()
|| c.Attribute("Dag").Value == thirdDay.Day.ToString() && c.Attribute("Måned").Value == thirdDay.Month.ToString() 

select new Bonn3()
{
Date = new DateTime(myDay.Year,int.Parse(c.Attribute("Måned").Value),int.Parse(c.Attribute("Dag").Value)),

Fajr = c.Attribute("Fajr").Value,
Soloppgang = c.Attribute("Soloppgang").Value,
Zohr = c.Attribute("Zohr").Value,
Asr = c.Attribute("Asr").Value,
Maghrib = c.Attribute("Maghrib").Value,
Isha = c.Attribute("Isha").Value,

};

listBox1.ItemsSource = filteredData;
Was it helpful?

Solution

The quintessence of what you could do is this:

int year = DateTime.Now.Year;
int lastMonth = 12;
var bonn3s = new List<Bonn3>();
foreach (var d in filtererData)
{
    int month = int.Parse(c.Attribute("Måned").Value);
    if (lastMonth - month < 0)
        year--;

    bonn3s.Add(new Bonn3()
    {
        Date = new DateTime(year,month,int.Parse(c.Attribute("Dag").Value)),

    ....
    lastMonth = month;
}

Edit

You could give Bonn3 a property DateString (or the like):

public string DateString
{
    get { return this.Date.ToShortDateString(); }
}

and bind to that for display purposes.

OTHER TIPS

Without the year in the data you'll have to add handling for a change in year yourself. This should just be a check against the month though so relatively straight forward.

You can change the way the date is displayed by formatting it when converting to a string.
See: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

Rather than using that LINQ statement for sorting, you may find it easier to use a CollectionViewSource.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top