score:1
First modify the class as per @abatischcev
public class PrayerTime
{
public TimeSpan Fajr { get; set; }
public TimeSpan Sunrise { get; set; }
public TimeSpan Zuhr { get; set; }
}
Then modify the linq query select part as:
select new PrayerTime()
{
Fajr = TimeSpan.Parse(c.Attribute("Fajr").Value),
Sunrise = TimeSpan.Parse(c.Attribute("Sunrise").Value),
Zuhr = TimeSpan.Parse(c.Attribute("Zuhr").Value)
};
then your check should be:
var obj = filteredData.First();
TimeSpan currentTime = myDay.TimeOfDay;
string result = String.Empty;
if (currentTime >= obj.Fajr && currentTime < obj.Sunrise)
{
result = "Fajar";
}
else if (currentTime >= obj.Sunrise && currentTime < obj.Zuhr)
{
result = "Zuhar";
}
textbox1.Text = result;
(By the way, Zuhr time should be between Zuhr and Asar :))
score:1
First, keep not string but TimeSpan object:
public TimeSpan Fajr { get; set; }
public TimeSpan Sunrise { get; set; }
To do this parse XML into DateTime:
TimeSpan ts = TimeSpan.Parse(c.Attribute("attr"));
So:
TimeSpan now = DateTime.Now.TimeOfDay; // time part only
var data = filteredData.First();
string result = null;
if (data.Fajr <= now && now < data.Sunrise); // notice operators greed
result = data.Fajr;
else if (data.Sunrise <= now && now <= data.Zuhr)
result = data.Zuhr;
myTextBox.Text = result;
score:0
The problem here is your code is "stringly typed". I would be better to use type that is design for time e.g. DateTime, but to quick fix it:
// don't you need a third one here?
select new PrayerTime()
{
Fajr = c.Attribute("Fajr").Value,
Sunrise = c.Attribute("Sunrise").Value,
};
Tu get current hour:
int currentHour = DateTime.Now.Hour;
Then is just simple comparison of two integers.
var data = filteredData.First();
int fajrHour = int.Parse(data.Fajr.Substring(0, 2), CultureInfo.InvariantCulture);
int sunriseHour = int.Parse(data.Sunrise.Substring(0, 2), CultureInfo.InvariantCulture);
int zuhrHour = int.Parse(data.Zuhr.Substring(0, 2), CultureInfo.InvariantCulture);
if(fajrHour <= currenHour && currenHour < sunriseHour)
{
myTextBox.Text = data.Fajr; // or to show value fajrHour.ToString()
}
if(sunriseHour <= currenHour && currenHour < zuhrHour)
{
myTextBox.Text = data.Zuhr; // zuhrHour.ToString()
}
// don't you need a third one here?
Source: stackoverflow.com
Related Articles
- How to make an if statement to show xml attribute based on current time
- How to do condition statement in LINQ based on code variables?
- unique, non-repeating alphanumeric code based on time ticks
- Display data in sorting order based on arrived time with current date using LINQ query
- How to make calculation on time intervals?
- How to discover financial Year based on current datetime?
- LINQ: Remove Element from XML based on attribute value?
- Filter large list based on date time
- Handling different databases with LINQ based off the user which is current logged in?
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- Filter values based on custom attribute in another table
- Improve the time complexity of current Linq queries
- How to make if statement inside linq
- MonoTouch Linq to XML how to make an IF statement inline
- Getting property value based on its column attribute value
- Make an Anonymous object for Linq Select statement
- Facebook Interview Question: Formatting a collection of times for a movie show time output (using Linq is preferred)
- Linq query based on attribute
- How to make combination based on limit provided?
- Entity Framework - LINQ statement with WHERE clause based on NULLABLE parameter
- Modifying dates in ASP.NET MVC C# based on stored time zone
- C# return value inside linq list based on if statement
- creating Linq to sqlite dbml from DbLinq source code
- How to make select statement Dynamic ? Linq
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- Code Cleanup: Best way to split long statement over multiple lines
- Make this LINQ statement return T instead of Task<T>
- can i make a this linq statement iteration from a collection in a single statement?
- Select First, Last, Min, Max Value based on Time From a List of Objects
- How to use Linq in C# to select a specific string from multiple nested columns?
- How to write multiple statments in select method of lambda expressions
- Linq-To-Entities subquery with string concatenation
- Optional requirement on where clause in LINQ
- How to get highest value from xml element in C#?
- LINQ to Entities does not recognize the method
- Linq GroupBy and select other attributes as list
- Unknown column 'Project2.Name' in 'where clause'
- entity framework grouping by datetime
- understanding of using linq to parse xml
- Use LINQ to retrieve largest number from Properties of a collection, within a collection
- LINQ join tables and count groups
- Unable to cast object of type 'System.Data.Common.DataRecordInternal' to type 'System.Data.IDataReader'
- LINQ query Joins
- Variable Includes in Linq to Sql EF statement
- Getting Error System.InvalidOperationException When Invoke WebService Method
- How can I compare two sets of data in linq and return the common data?
- LINQ How to force query to materialize?
- Using XDocument to build XHTML with nested tags
- Can't "Enable Editing" on my gridview