Storing actual repeating date's without the year part - c#

Using the Entity Framework data access model, I am attempting to store Clubs including their meeting day/time information. Instead of using a free text field for the user to write something on the lines of "We meet on a Wednesday 5-6 and Thursday 7-8" I instead wish to store the meeting day and times in a more meaningful format.
I have a Club model something along the lines of:
public class Club
{
public bool VisuallyImpairedWelcome { get; set; }
public string Explanation { get; set; }
public List<CalendarEntry> MeetingDays { get; set; }
public DateTime DateCreated { get; set; }
}
CalendarEntry model looks like:
public class CalendarEntry
{
public int CalendarEntryId { get; set; }
public int ClubId { get; set; }
public Days Day { get; set; }
public DateTime From { get; set; }
public DateTime To { get; set; }
public Intervals Interval { get; set; }
public List<ExceptionDays> ExceptionDays { get; set; }
public string AdditionalInfo { get; set; }
}
The problem im having is with the List<ExceptionDays>, my thinking here would be the user could set up their regular meeting days i.e:
Monday 17:00 - 18:30
Wednesday 17:30 - 18:30
Occurrence: Every Week
Except Yearly: 25th December 26th December
Something along these lines, the problem is I have no idea how I should construct my ExceptionDays class, something along the lines of (pseudo:)
public class ExceptionDays
{
int Day { get; set; }
int Month { get; set; }
}
But obviously the Day/Month combination must be possible, and the date is to be excluded as this will be a re-occurring date.
Firstly am I taking the right approach here, if so how can I construct a water tight ExceptionDays class?

Related

Using part of the Date in a ViewSource Grouping Description WPF C#

I have a ViewSource based on an observable collection of a set of objects based on a c# class called "Ticket" for a DataGrid for a WPF project in C# .net 4. The project is for a simple Helpdesk app.
The ViewSource Grouped on a Property call TktDate and all the tickets entered that day obviously grouped together in my DataGrid
However I have recently updated the code so the TktDate now not only stores the Date element but also the Time element, so clearly when I mean to Group on the Date only I am failing to do this because of the time elements (Am I correct?).. So if that was not clear here is a screen shot
Is there a way to Group on the date part TktDate and ignore the time element for my ViewSource or must I add a new property to my class to store the date part only and group on the new property?
See the properties of the class below
public class Ticket : INotifyPropertyChanged
{
public int userid { get; set; }
public int deptid { get; set; }
public int topicid { get; set; }
public int staffid { get; set; }
public int priorityid { get; set; }
public string poster { get; set; }
public DateTime TktDate { get; set; }
public string title { get; set; }
public string bodyopen { get; set; }
public string bodyclose { get; set; }
public string timespent { get; set; }
public string dayoffset { get; set; }
public string sysdt { get; set; }
public string Netdt { get; set; }
public string Teldt { get; set; }
public string Clidt { get; set; }
public string status { get; set; }
private string ticket;
public string Tket
In the ViewSource I group on the TktDate, which records the date the ticket is created. The grouping code is below and it worked fine
CollectionViewSource ticketViewSource = ((CollectionViewSource)(this.FindResource("ticketViewSource")));
ticketViewSource.Source = TicketCol;
ticketDataGrid.DataContext = ticketViewSource;
//add grouping
if (ticketViewSource != null)
{
ticketViewSource.GroupDescriptions.Clear();
ticketViewSource.GroupDescriptions.Add(new PropertyGroupDescription("TktDate"));
ticketViewSource.SortDescriptions.Clear();
ticketViewSource.SortDescriptions.Add(new SortDescription("TktDate", ListSortDirection.Ascending));
}
Is there a way to Group on the date part TktDate and ignore the time element for my ViewSource or must I add a new property to my class to store the date part only and group on the new property?
You may try to group by a nested property:
ticketViewSource.GroupDescriptions.Add(new PropertyGroupDescription("TktDate.Date"));
If this doesn't work you should add a new DateTime read-only property to your Ticket class that returns TktDate.Date and group by this one.

Which design pattern will be best for football match app

I am creating web api for application which support football team in collecting statistic data from matches. I am on implementing phase now. And lets say I am thinking what (if it is needed) type of design patter will be best for something like this:
public class Shot
{
public int Id { get; set; }
public int PlayerId { get; set; }
public string Comment { get; set; }
public bool OnGoal { get; set; }
public int GameId { get; set; }
}
and
public class Card
{
public int Id { get; set; }
public int PlayerId { get; set; }
public string Comment { get; set; }
public bool IsRed{ get; set; }
public int GameId { get; set; }
}
As You can see some properties are same. It should be implemented with interface, inheritance ( f.e. class Action) or maybe I should use one of Design patterns (which one)? What will be best for Entity Framework to avoid problems in later phases?
Well, both your classes represent some kind of game event - shot and card. There could be some other game events, like a free kick, throw in, substitution, penalty, or corner kick. All of these events should have id, game id, player id, timestamp, and probably comment. So your problem is the duplication of data in several classes. It is easily solved with inheritance. No patterns required:
public abstract class GameEvent
{
public int Id { get; set; }
public int GameId { get; set; }
public int PlayerId { get; set; }
public TimeSpan Time { get; set; }
public string Comment { get; set; }
}
And various specific events
public class Shot : GameEvent
{
public bool OnGoal { get; set; }
}
public class Card : GameEvent
{
public bool IsRed { get; set; }
}
You should also think about saving timestamp of added time because you can get both 46 minutes time span (start of second half) and 45+1 minutes of first half.

Presenting data from database without multiplying specific field

I have something like this in database:
and I want to display it like this:
date1 worktime UserName1
date2 worktime
date3 worktime
date1 worktime UserName2
date2 worktime
date3 worktime
I've cereated a Model:
[Table("Work_Hours")]
public class User
{
public string UserID { get; set; }
public string UserName { get; set; }
public TimeSpan WorkTime { get; set; }
public DateTime Date { get; set; }
}
public class UserDBContext : DbContext
{
public DbSet<User> Users { get; set; }
}
And tried to solve my problem with ViewModel:
public class UserViewModel
{
public string UserID { get; set; }
public string UserName { get; set; }
public LinkedList<TimeSpan> WorkTime { get ; set;}
public LinkedList<DateTime> Date { get; set; }
}
But I don't know how add only one object and list of WorkTime and Date to new list of viewmodels for one specific UserName from list of models.
Linq already has this built in. Given:
IEnumerable<User> data=...; // input from somewhere
You can do:
var groups=data.GroupBy(w=>w.UserName); // type is IGrouping<string, User>
Enumerating this object will give you a Key property which is the username, once for each item, then enumerating this returning object again will give you every item that has the specified username.

Week and day entities

I am learning C# and MVC with ASP.NET. I am making an application that takes daily data and splits it up into weeks.
My question is: How should I go about making the entities? I want it so that you have a WeekNo and a DayNo and you use them to display, edit and create data and tables. So for example, WeekNo = 1 and DayNo = 1 would be Monday in week 1. WeekNo = 1 and DayNo = 2 would be Tuesday and so on.
I'm new to this and not very good at information management. Do I need foreign keys?
I was thinking:
public class Day
{
public int WeekNo { get; set; }
public int DayID { get; set; }
public int DayNo { get; set; }
public string DayofWeek { get; set; }
//then other declarations, not important
[ForeignKey("DayNo")]
}
public class Week
{
public int ID { get; set; }
public int WeekNo { get; set; }
public int DayofWeek { get; set; }
}
Does the foreign key go in Day or Week or do I even need it?
On the assumption that you meant Objects when saying entities.
Here is how you can structure an Object for use per day:
public class myDailyObject
{
public myDailyObject()
{ }
public int ObjectID { get; set; }
public int WeekNo { get; set; }
public int DayNo { get; set; }
public string DayofWeek { get; set; }
//Other variables can go here.
}
Otherwise, you can use the following structure for use per week:
public class myWeeklyObject
{
public myWeeklyObject()
{
DataByDay = new Dictionary<int, myDataObject>();
}
public int ObjectID { get; set; }
public int WeekNo { get; set; }
public Dictionary<int, myDataObject> DataByDay { get; set; }
}
public class myDataObject
{
//Other variables can go here.
}
In here, you can use the Dictionary<int, myDataObject> called DataByDay to contain an instance of the myDataObject Object for each day of the week.
Example of usage:
The Weekly object:
//Declare the weekly object:
myWeeklyObject thisWeek = new myWeeklyObject();
//Populate variables of thisWeek here:
thisWeek.ObjectID = thisWeek.WeekNo = CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
//The line above gets the current weekNo for the year.
You could also fetch this object from somewhere if one already exists for this week.
So now, the daily object:
//Get the day of the week as an int:
int dayOfWeek = (int)DateTime.Now.DayOfWeek + 1; //DayOfWeek is 0 based
//Daclare object for a specific day:
myDataObject newObject = new myDataObject();
//Populate variables of newObject here.
//Then add it with the int dayOfWeek as the Key to the DataByDay Dictionary.
thisWeek.DataByDay.Add(dayOfWeek, newObject);
I hope this answers your question (and future questions such as how to get the WeekNo), or at least is of some kind of help.
Good luck.

(N)Hibernate Criteria: summing multiple distinct columns

None of the similarly worded questions on SO seem to match, and googling pretty much points to SO for this, so let's try this:
I have a JournalEntry entity class that looks like this:
public partial class JournalEntry
{
public virtual Guid JournalEntryId { get; set; }
public virtual Account Account { get; set; }
public virtual decimal DebitAmount { get; set; }
public virtual decimal CreditAmount { get; set; }
[NotNull]
public virtual DateTime EffectiveDate { get; set; }
[NotNull]
public virtual DateTime PostingDate { get; set; }
public virtual UserProfile PostedBy { get; set; }
[FullTextIndexed]
public virtual string Notes { get; set; }
public virtual Amortization Amortization { get; set; }
public virtual ExpenseCategories ExpenseCategory { get; set; }
[Index]
public virtual bool IsClosed { get; set; }
}
I also have a simple class for holding transaction summaries like so:
public class JournalEntrySummary
{
public decimal Credits { get; set; }
public decimal Debits { get; set; }
}
What I would like to do is write a Criteria query that will return the sums of both the Credits property and the Debits property. IOW, I would like something vaguely shaped like this SQL query:
select
sum(creditamount) as Credits,
sum(debitamount) as Debits
from
journalentries
where
...
... and have that populate my JournalEntrySummary object. I've seen lots of examples of how to do one column, and even some examples of adding the two columns together, but no examples of collecting the two distinct summaries and dumping them into a non-domain object.
Is this possible? How would I do it?
Take a look at criteria queries documentation http://nhibernate.info/doc/nh/en/index.html#querycriteria-projection, there are some examples that will answer you question.
For your example you should try this
// using NHibernate.Criterion;
// using NHibernate.Transform;
session.CreateCriteria<JournalEntry>()
.SetProjection(
Projections.Sum<JournalEntry>(x => x.DebitAmount).As("Debits"),
Projections.Sum<JournalEntry>(x => x.CreditAmount).As("Credits"),
// you can use other aggregates
// Projections.RowCount(),
// Projections.Max<JournalEntry>(x => x.EffectiveDate)
)
.SetResultTransformer(Transformers.AliasToBean<JournalEntrySummary>())
.UniqueResult<JournalEntrySummary>();

Categories