Multiple Reminders in Windows Phone - c#

Getting InvalidOperationException or sometimes TargetInvocationException, When Control reaches at this function the Exception thrown.
private void setreminders()
{
DateTime Date1 = new DateTime(2014, 02, 10, 06, 00, 00);
DateTime Date2 = new DateTime(2014, 02, 10, 07, 30, 00);
Reminder _Reminder1 = new Reminder("TodoReminder")
{
BeginTime = Date1,
Title = "Diet Reminder",
Content = "Its time to wake up, Have a Glass of Water and Start Excercising like YOGA & PRANAYAMA",
};
_Reminder1.RecurrenceType = RecurrenceInterval.Daily;
ScheduledActionService.Add(_Reminder1);
Reminder _Reminder2 = new Reminder("TodoReminder")
{
BeginTime = Date2,
Title = "Diet Reminder",
Content = "Drink a Glass of Water",
};
_Reminder2.RecurrenceType = RecurrenceInterval.Daily;
ScheduledActionService.Add(_Reminder2);
}

Related

Getting error Year, Month, and Day parameters describe an un-representable DateTime Exception

I writted code for automatical changer of shifts. Night shift, morning shift, etc. Everything works fine. Current problem with this is when is new month. 5 Month has 31 days. This month have 30 days.
My current code is this:
DateTime lastTime = _lastDateOfLoading;
DateTime tempTime = Convert.ToDateTime(hourOfLoading);
DateTime targetTime = new DateTime(DateOfLoading.Year, DateOfLoading.Month, DateOfLoading.Day, tempTime.Hour, tempTime.Minute, tempTime.Second);
DateTime currentTime = DateTime.Now;
bool tt1 = false;
bool tt2 = false;
bool lt1 = false;
bool lt2 = false;
DateTime lt1Min = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 22, 00, 00);
DateTime lt1Max = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 23, 59, 59, 999);
DateTime lt2Min = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 00, 00, 00);
DateTime lt2Max = new DateTime(currentTime.Year, currentTime.Month, lastTime.Day, 06, 00, 00);
DateTime tt1Min = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 22, 00, 00);
DateTime tt1Max = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 23, 59, 59, 999);
DateTime tt2Min = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 00, 00, 00);
DateTime tt2Max = new DateTime(currentTime.Year, currentTime.Month, targetTime.Day, 06, 00, 00);
if ((lastTime >= lt1Min
&& (lastTime < lt1Max)))
{
lt1 = true;
}
etc ..
It end on lt1Min = new DateTime ...
I think it happen because is june. And lastTime is 22.5.
Any advice how to solve this thing? Nothing helped on current threads here on stackoverlow.
Thank you.

Find hole in schedule

I’m looking for a way to find holes in a schedule, times that does not have a booking.
I have a simple class in C# that looks like:
DateTime StartTime { get; set; }
Datetime EndTime { get; set; }
public int User_ID { get; set; }
The same class is used for the bookings aswell.
Let’s assume I have these objects:
Schedule: StartTime "2017-03-14 08:00" - EndTime "2017-03-14 16:00" (8 hours)
Booking: StartTime "2017-03-14 09:00" - Endtime "2017-03-14 10:00" (1 hour)
My final result from this would be 2 objects that represents the “free time”:
Free: StartTime "2017-03-14 08:00" EndTime: "2017-03-14 09:00" (1 hour)
Free: StartTime "2017-03-14 10:00" EndTime: "2017-03-14 16:00"(6 hour)
How would I check this in C#?
I'm thinking about looping the Schedule and split them on start/end of each booking, but I'm not sure how to do it.
It is easier than I thought... Note that this code isn't optimized, and this algorithm isn't probably very optimizable:
public class TimeSegment
{
public readonly DateTime StartTime;
public readonly DateTime EndTime;
public TimeSegment(DateTime startTime, DateTime endTime)
{
StartTime = startTime;
EndTime = endTime;
}
public TimeSegment[] Subtract(TimeSegment other)
{
// 8-10 Subtract 10-11 = 8-10
if (StartTime > other.EndTime || other.StartTime > EndTime)
{
// If there is no intersection, we return { this }
// (no subtraction)
return new[] { this };
}
if (StartTime >= other.StartTime)
{
// 8-10 Subtract 8-10 = (nothing)
// 8-10 Subtract 7-11 = (nothing)
if (EndTime <= other.EndTime)
{
// Total subtraction, nothing remains!
return new TimeSegment[0];
}
else
{
// 8-10 Subtract 7-9 = 9-10
return new[] { new TimeSegment(other.EndTime, EndTime) };
}
}
// 8-12 Subtract 9-13 = 8-9
if (EndTime <= other.EndTime)
{
return new[] { new TimeSegment(StartTime, other.EndTime) };
}
// 8-12 Subtract 9-11 = 8-9, 11-12
// Complete case: two TimeSegments returned
return new[] { new TimeSegment(StartTime, other.StartTime), new TimeSegment(other.EndTime, EndTime) };
}
public override string ToString()
{
return string.Format("{0}-{1}", StartTime, EndTime);
}
}
And then:
var schedules = new List<TimeSegment> { new TimeSegment(new DateTime(2017, 03, 14, 08, 00, 00), new DateTime(2017, 03, 14, 16, 00, 00)) };
var bookings = new List<TimeSegment>
{
new TimeSegment(new DateTime(2017, 03, 14, 09, 00, 00), new DateTime(2017, 03, 14, 10, 00, 00)),
new TimeSegment(new DateTime(2017, 03, 14, 12, 00, 00), new DateTime(2017, 03, 14, 14, 00, 00)),
new TimeSegment(new DateTime(2017, 03, 14, 13, 00, 00), new DateTime(2017, 03, 14, 15, 00, 00)),
};
foreach (TimeSegment booking in bookings)
{
var schedulesNew = new List<TimeSegment>();
foreach (TimeSegment schedule in schedules)
{
var diff = schedule.Subtract(booking);
schedulesNew.AddRange(diff);
}
schedules = schedulesNew;
}
There "core" of this is a Subtract function that given a TimeSegment subtracts from this another TimeSegment, returning 0, 1 or 2 TimeSegments... Then iteratively we subtract all the bookings from the TimeSegments that we produced from the previous booking.

How do I use LINQ to compare a date and a datetime?

I'm trying to seed a database with two lists. The first list is just a bunch of items. The second list is a bunch of junk that references the first list. I'm trying to reference items from first list in the second list of junk, via LINQ, but I'm fairly certain I'm not doing it right:
For example, List 1:
var items = new List<Item>()
{
new Item { ItemtId = 1, DateTime = new DateTime(2014, 09, 05, 12, 30, 30), Text = "Cheese" },
new Item { ItemtId = 1, DateTime = new DateTime(2014, 09, 05, 12, 30, 30), Text = "Lettuce" },
new Item { ItemtId = 1, DateTime = new DateTime(2014, 09, 05, 12, 30, 30), Text = "Ground Beef" },
new Item { ItemtId = 1, DateTime = new DateTime(2014, 09, 03, 12, 30, 30), Text = "Ketchup" },
new Item { ItemtId = 1, DateTime = new DateTime(2014, 09, 03, 12, 30, 30), Text = "Mustard" },
};
var junk= new List<Junk>()
{
new Junk { JunkId = 1, DateTime = new DateTime(2014, 09, 05, 10, 00, 00), Items = items.Where(d => d.DateTime.ToShortDateString() == new DateTime(2014, 09, 05).ToShortDateString()},
new Junk { JunkId = 2, DateTime = new DateTime(2014, 09, 03, 11, 00, 00), Items = items.Where(d => d.DateTime.ToShortDateString() == new DateTime(2014, 09, 03).ToShortDateString()}
};
This seems like it should be the answer to me, because I'm only interested in the date and not the time, but it doesn't seed like this. I get the error:
LINQ to Entities does not recognize the method 'System.String
ToShortDateString()' method, and this method cannot be translated into
a store expression.
Any thoughts on how to build a better solution?
UPDATE
Looks like I didn't transcribe my code accurately to the site, and ended up finding my problem.
In my original code, I had:
new Junk { JunkId = 1, DateTime = new DateTime(2014, 09, 05, 10, 00, 00), Items = context.Items.Where(d => d.DateTime.Date == new DateTime(2014, 09, 05) }
When I should have had
new Junk { JunkId = 1, DateTime = new DateTime(2014, 09, 05, 10, 00, 00), Items = items.Where(d => d.DateTime.Date == new DateTime(2014, 09, 05) }
I'd been flirting with .ToShortDateString() unnecessarily
Change filter to:
.Where(d => d.DateTime.Date == new DateTime(2014, 09, 05))
You can use TruncateTime function of EntityFunctions Class.
new Junk { JunkId = 1, DateTime = new DateTime(2014, 09, 05, 10, 00, 00), Items = items.Where(d => EntityFunctions.TruncateTime(d.DateTime) == EntityFunctions.TruncateTime(new DateTime(2014, 09, 05))}
Note: If you using EntityFramework 6 then it should be System.Data.Entity.DbFunctions.TruncateTime(...) method from EntityFramework.dll.
Your subquery (the Items = part of the Junk variable) isn't a list but an enumerable, so it doesn't get materialized untill it's enumerated, and it's not enumerated untill you use it, down the road, in your linq to entities query.
If you change it as such :
var junk= new List<Junk>()
{
new Junk { JunkId = 1, DateTime = new DateTime(2014, 09, 05, 10, 00, 00), Items = items.Where(d => d.DateTime.ToShortDateString() == new DateTime(2014, 09, 05).ToShortDateString()).ToList()}, // this one will work as it gets materialized right now, not later, so by the time you pass it to linq to entities later it will already be a simple list of diferences, it won't try to compute "toshortdatestring" later, that will already be done
new Junk { JunkId = 2, DateTime = new DateTime(2014, 09, 03, 11, 00, 00), Items = items.Where(d => d.DateTime.ToShortDateString() == new DateTime(2014, 09, 03).ToShortDateString()}
};

Sort Date time only by start date

I have already asked a different question regarding Sorting Date Time and got help from another user to pass my values. I am using a for loop like below, but definitely am wrong here because the code brings the value one by one rather than sorting.
public class Break
{
public DateTime MealStart { get; set; }
public DateTime MealEnd { get; set; }
}
my main class
IList<DateTime> starts = new List<DateTime>();
IList<DateTime> ends = new List<DateTime>();
DateTime breakStart1 = new DateTime(2012, 02, 15, 12, 30, 00); // 15/02/12 12.30PM
DateTime breakEnd1 = new DateTime(2012, 02, 15, 13, 30, 00); // 15/02/12 01.30PM
DateTime breakStart2 = new DateTime(2012, 02, 15, 11, 00, 00); // 15/02/12 11.00AM
DateTime breakEnd2 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakStart3 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakEnd3 = new DateTime(2012, 02, 15, 01, 00, 00); // 15/02/12 01.00PM
starts.Add(breakStart1);
starts.Add(breakStart2);
starts.Add(breakStart3);
ends.Add(breakEnd1);
ends.Add(breakEnd2);
ends.Add(breakEnd3);
for (int i = 0; i < starts.Count; i++)
{
var breaks = new List<Break>()
{
//for (int j= 0; j<starts.Count; j++)
//{
new Break()
{
MealStart = starts[i],
MealEnd = ends[i]
}
// }
};
var ordered = breaks.OrderBy(s => s.MealStart);
foreach (var ord in ordered)
{
System.Console.WriteLine(ord.MealStart);
System.Console.WriteLine(ord.MealEnd);
}
}
I am expecting a result like below
breakStart1 = 15/02/12 11.00AM
breakEnd1= 15/02/12 12.00PM
breakStart2 = 15/02/12 12.00PM
breakEnd2= 15/02/12 01.00PM
breakStart3 = 15/02/12 12.30PM
breakEnd3= 15/02/12 01.30PM
but it's not because of the for loop.
You are creating breaks after ever loop, you need to do this outside of the loop like this:
IList<DateTime> starts = new List<DateTime>();
IList<DateTime> ends = new List<DateTime>();
DateTime breakStart1 = new DateTime(2012, 02, 15, 12, 30, 00); // 15/02/12 12.30PM
DateTime breakEnd1 = new DateTime(2012, 02, 15, 13, 30, 00); // 15/02/12 01.30PM
DateTime breakStart2 = new DateTime(2012, 02, 15, 11, 00, 00); // 15/02/12 11.00AM
DateTime breakEnd2 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakStart3 = new DateTime(2012, 02, 15, 12, 00, 00); // 15/02/12 12.00PM
DateTime breakEnd3 = new DateTime(2012, 02, 15, 01, 00, 00); // 15/02/12 01.00PM
starts.Add(breakStart1);
starts.Add(breakStart2);
starts.Add(breakStart3);
ends.Add(breakEnd1);
ends.Add(breakEnd2);
ends.Add(breakEnd3);
List<Break> breaks = new List<Break>();
for (int i = 0; i < starts.Count; i++)
{
breaks.Add(new Break()
{
MealStart = starts[i],
MealEnd = ends[i]
});
}
var ordered = breaks.OrderBy(s => s.MealStart);
foreach (var ord in ordered)
{
System.Console.WriteLine(ord.MealStart);
System.Console.WriteLine(ord.MealEnd);
}
Since #Corylulu beat me to the punch on the basic issue, here's a different method that is slightly shorter:
IEnumerable<Break> breaks =
starts.Zip(ends, (s, e) => new Break { MealStart = s, MealEnd = e })
.OrderBy(b => b.MealStart);
foreach (Break brk in breaks)
Console.WriteLine("Start: {0}\tEnd: {1}", brk.BreakStart, brk.BreakEnd);
The IEnumerable.Zip method takes a pair of IEnumerables and a transform function and produces an output IEnumerable containing the results of calling the transform function with members of each input IEnumerable. You could convert it to a List<Break> with a ToList() at the end of course.

How can I get year from date

I want to get year from date in mongodb.
This is my simple script :
DateTime sDate = DateTime.SpecifyKind(new DateTime(2013, 1, 15, 00, 00, 00), DateTimeKind.Utc);
DateTime eDate = DateTime.SpecifyKind(new DateTime(2014, 12, 31, 23, 59, 59), DateTimeKind.Utc);
string map = # "function(){
emit (_id.OwnerId, _id.Date {
Date: Value.Date.getYear(),
PurchaseAmount: parseFloat(Value.PurchaseAmount),
PurchaseReturnAmount: parseFloat(Value.PurchaseReturnAmount),
TotalAmount: parseFloat(Value.TotalAmount)
});
}";
string reduce = # "function (key, values) {
var outValue = {purchsaeAmount:0 , purchaseReturnAmount:0, totalAmount:0 }
values.forEach(function (value) {
outValue.PurchaseAmount +=parseFloat(value.PurchaseAmount);
outValue.PurchaseReturnAmount +=parseFloat(value.PurchaseReturnAmount);
outValue.TotalAmount +=parseFloat(value.TotalAmount);
});
return outValue;
}";
In map I want to get only year, how can I get start year(2013) and end year(2014)?
var d = new Date();
var n = d.getFullYear();
var next_year = d.getFullYear()+1
out:
2013 2014

Categories

Resources