我开始用C#.NET编程。我为工人制作了一个网络日历,他们可以查看由于疾病,假期等而未上班的天数。 起初我用2个数组做了。一个用于具有这些尺寸的完整日历:
calendar[person_code][month][day][6 items] -> calendar[int][int][int][object (starting absence date, type of absence, etc)]
另一个是具有一个工作定义的数组,如
person[person_code][13 items] -> person[int][13 string (name, surname, etc)]
我想用类来改进我的代码。我在考虑上面的课程:
public class absence
{
public int absenceCode { get; set; }
public int typeAbsence { get; set; }
public DateTime startingDate { get; set; }
public DateTime endingDate { get; set; }
public string description { get; set; }
public string state { get; set; }
public constructors...
}
public class calendar
{
public int day { get; set; }
public int month { get; set; }
public absence absen { get; set; }
public contructors....
}
public class person
{
public int personCode { get; set; }
public string surname { get; set; }
public string name { get; set; }
public string address { get; set; }
public int maxHolidayDays { get; set; }
public calendar calend { get; set; }
public constructors....
}
这是我的代码,但我不知道如何设置和检索信息,如使用数组。 我可以为每个人创建一个新的人物实例,但我需要在一个独特的日子里创造一些缺席。 (一名工人一天内有时可能缺勤) 我曾经使用像
这样的List(T)类 List<person> = new List<person>(), and List<absence> = new List<absence>()
但是如何在一天的“日历”课程中设置List<absence>()
?
我需要在列表中的某个人的日历日内设置该列表:/
List<person> -> calendar -> List<absence>
我希望这对你来说足够清楚。 问候。
答案 0 :(得分:0)
我会这样做。 1.Skip日历课程 2.在人员班级中添加缺席集合(例如列表) 3.如果你有IEnumerable(例如List,person []),你可以搜索你需要的数据
public class absence
{
public int absenceCode { get; set; }
public int typeAbsence { get; set; }
public DateTime startingDate { get; set; }
public DateTime endingDate { get; set; }
public string description { get; set; }
public string state { get; set; }
}
public class person
{
public int personCode { get; set; }
public string surname { get; set; }
public string name { get; set; }
public string address { get; set; }
public int maxHolidayDays { get; set; }
// Initialize in constructor if you have an error
public List<absence> absences { get; set; } = new List<absence>();
}
至于搜索,这将非常容易。让我们说你需要在特定日期缺席的人:
IEnumerable<person> Persons = GetPersonsList();
DateTime dateToCheck = GetAbsentDate();
var absentPersons = Persons.Where(x=>x.absences.Any(a=>a.startingDate>= dateToCheck&&a.endingDate<= dateToCheck));
答案 1 :(得分:0)
首先,正如问题评论中提到的user3185569,您已使用此说明更改了代码:Capitalization Conventions
其次,您必须考虑数据表示。如果人和缺席之间的关系变成什么样的?一个人可能有几个缺席(1:N关系)。因此,您必须存储有关缺席的数据以及与此缺席相关的人员。
类:
public class Person
{
public int PersonCode { get; set; }
public string Surname { get; set; }
public string Name { get; set; }
public string Address { get; set; }
//constructor, etc.
public override string ToString()
{
return string.Format("{0} {1} {2} {3}", PersonCode, Surname, Name, Address);
}
}
public class Absence
{
public int AbsenceCode { get; set; }
public int TypeAbsence { get; set; }
public DateTime StartingDate { get; set; }
public DateTime EndingDate { get; set; }
public string Description { get; set; }
public string State { get; set; }
//"a link" to person
public Person Person { get; set; }
//constructor, etc.
public override string ToString()
{
return string.Format("{0}\t|\t{1}\t{2}\t{3}", Person.ToString(), TypeAbsence.ToString(),
StartingDate.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture),
EndingDate.ToString("yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture));
}
}
用法:
List<Person> persons = new List<Person>()
{
new Person() {PersonCode = 1, Surname = "Los", Name = "Maciej", Address = "Poland, ..."},
new Person() {PersonCode = 2, Surname = "Doe", Name = "John", Address = "USA, ..."},
new Person() {PersonCode = 3, Surname = "McAlister", Name = "Fred", Address = "UK, ..."},
new Person() {PersonCode = 4, Surname = "Sommer", Name = "Anna", Address = "Canada, ..."},
};
List<Absence> absences = new List<Absence>()
{
new Absence(){AbsenceCode = 1, TypeAbsence=1, StartingDate = new DateTime(2017,5,11), EndingDate = new DateTime(2017,5,15),
Description = "whatever", State = "A", Person = persons[0]},
new Absence(){AbsenceCode = 2, TypeAbsence=1, StartingDate = new DateTime(2017,5,18), EndingDate = new DateTime(2017,6,8),
Description = "whatever", State = "A", Person = persons[1]},
new Absence(){AbsenceCode = 3, TypeAbsence=2, StartingDate = new DateTime(2017,6,1), EndingDate = new DateTime(2017,6,12),
Description = "whatever", State = "B", Person = persons[2]},
new Absence(){AbsenceCode = 4, TypeAbsence=2, StartingDate = new DateTime(2017,6,1), EndingDate = new DateTime(2017,6,5),
Description = "whatever", State = "B", Person = persons[0]},
new Absence(){AbsenceCode = 5, TypeAbsence=1, StartingDate = new DateTime(2017,6,2), EndingDate = new DateTime(2017,6,5),
Description = "whatever", State = "A", Person = persons[2]}
};
//define date-range to filter Absences data
DateTime dfrom = new DateTime(2017,6,5);
DateTime dTo = new DateTime(2017,6,9);
var LastWeekAbsencesByPerson = absences
.Where(x=> (x.StartingDate<=dTo) && (x.EndingDate>=dfrom))
.OrderBy(x => x.Person.Surname)
.ThenBy(x => x.StartingDate)
.ToList();
foreach(var lwabp in LastWeekAbsencesByPerson)
{
Console.WriteLine("{0}", lwabp.ToString());
Console.WriteLine("{0}", new string('-', 60));
}
以同样的方式,您可以按日期,人员,缺席类型等过滤数据
关于日期过滤器,我强烈建议您阅读:Determine Whether Two Date Ranges Overlap
有很多事情要做,要改进,但这是你的工作。
祝你好运!