Based on date, get todays and the next two days va

2019-03-06 10:47发布

Goal: Based on todays date and month, get actual and the next two days values from XML.

Issue: Although my c.Attribute("Day").Value changes, my c.Attribute("Month").Value stays the same. So if actual day i 30.04.2012 then it will show prayertimes for 30.04.2012 but not 01.05.2012 and 02.05.2012. How to solve this?

Also i am not sure if this Dato = c.Attribute("Day").Value + "." + c.Attribute("Month").Value + "." + myDay.Year.ToString(), is any good? I want the listbox to show the date of the xml it is getting.

Please help. My code, XML and class below.

var filteredData3 = from c in loadedCustomData.Descendants("PrayerTime")
    where int.Parse(c.Attribute("Day").Value) >= myDay.Day && int.Parse(c.Attribute("Day").Value) < (myDay.Day + 3) 
    && c.Attribute("Month").Value == myDay.Month.ToString()


        select new Bønn()
        {

        Dato = c.Attribute("Day").Value + "." + c.Attribute("Month").Value + "." + myDay.Year.ToString(),
        Fajr = TimeSpan.Parse(c.Attribute("Fajr").Value),
        Sunrise = TimeSpan.Parse(c.Attribute("Sunrise").Value),
        Zohr = TimeSpan.Parse(c.Attribute("Zohr").Value),
        Asr = TimeSpan.Parse(c.Attribute("Asr").Value),
        Maghrib = TimeSpan.Parse(c.Attribute("Maghrib").Value),
        Isha = TimeSpan.Parse(c.Attribute("Isha").Value),
        Jumma = TimeSpan.Parse(c.Attribute("Jumma").Value),

        };

listBox1.ItemsSource = filteredData3;

Here is my class:

public class Bønn
{

    public TimeSpan Fajr { get; set; }
    public TimeSpan Sunrise { get; set; }
    public TimeSpan Zohr { get; set; }
    public TimeSpan Asr { get; set; }
    public TimeSpan Maghrib { get; set; }
    public TimeSpan Isha { get; set; }
    public TimeSpan Jumma { get; set; }

    public string Dato { get; set; }

}

Here is my XML:

<PrayerTime
        Dag ="30" 
        Måned="4" 
        Fajr="04:09" 
        Sunrise="05:19" 
        Zohr="13:19" 
        Asr="18:30"
        Maghrib="21:14" 
        Isha="22:24" 

    />
    <PrayerTime
        Dag ="1" 
        Måned="5" 
        Fajr="04:08" 
        Sunrise="05:16" 
        Zohr="13:19" 
        Asr="18:31"
        Maghrib="21:17" 
        Isha="22:25" 

    />
    <PrayerTime
        Dag ="2" 
        Måned="5" 
        Fajr="04:06" 
        Sunrise="05:13" 
        Zohr="13:19" 
        Asr="18:33"
        Maghrib="21:19" 
        Isha="22:27" 
    />

1条回答
可以哭但决不认输i
2楼-- · 2019-03-06 11:18

I would suggest you change your "model" class to use DateTime instead of string for the date. Transform all the elements into your model class, and then filter. It'll be a lot simpler than trying to do arithmetic based on the attributes.

Also note that using the explicit conversions from XAttribute is simpler than calling int.Parse everywhere. I would actually suggest creating a static FromXElement method in your model class, so you can write:

DateTime start = DateTime.Today;
// We'll use this as an *exclusive* upper bound
DateTime end = start.AddDays(3);

var query = from c in loadedCustomData.Descendants("PrayerTime")
            let bonn = Bønn.FromXElement(c)
            where bonn.Dato >= start && bonn.Dato < end;
            select bonn;

Or in extension method syntax:

// start and end as before
var query = loadedCustomData.Descendants("PrayerTime")
                .Select(c => Bønn.FromXElement(c))
                .Where(bonn => bonn.Dato >= start && bonn.Dato < end);
查看更多
登录 后发表回答