How to populate array element defined inside the class? I would like to Populate List of student with array of Marks,which i am not able to locate .
public class Marks
{
public int ENG { get; set; }
public int MATHS { get; set; }
}
public class Student
{
public string empName { set; get; }
public string empAddress { set; get; }
public Marks[] StudentMarks { set; get; }
}
var objs = new List<Student>()
{
new Employee() {empName = "Manish" empAddress = "MUM"....array element with Marks of two subjects
new Employee() {empName = "Manoj", empAddress = "MUM"....arrayelement with Marks of two subjects
}
In order to initialize array in a single line you should use one of the following code
int[] n1 = new int[4] {2, 4, 6, 8};
int[] n2 = new int[] {2, 4, 6, 8};
int[] n3 = {2, 4, 6, 8};
In your case, you should populate your array with Marks objects.
Marks[] MarksArray = new [] {new Marks() {ENG = 1, MATHS = 1}, new Marks() {ENG = 2, MATHS = 2}};
This array contains 2 marks - new Marks() {ENG = 1, MATHS = 1} and new Marks() {ENG = 2, MATHS = 2}.
You can read find more examples here - https://msdn.microsoft.com/en-us/library/aa287601(v=vs.71).aspx
So you can create your objects this way
var objs = new List<Student>()
{
new Student() {empName = "Manish", empAddress = "MUM", StudentMarks = new [] {new Marks() {ENG = 1, MATHS = 1}, new Marks() {ENG = 2, MATHS = 2}} },
new Student() {empName = "Manish", empAddress = "MUM", StudentMarks = new [] {new Marks() {ENG = 1, MATHS = 1}, new Marks() {ENG = 2, MATHS = 2}} },
};
If you were concerned with the syntax only then this would do:
void Main()
{
var objs = new List<Employee>()
{
new Employee() {
empName = "Manish",
empAddress = "MUM",
StudentMarks = new Marks[] {
new Marks {ENG=10, MATHS = 100},
new Marks { ENG=20, MATHS = 80}, }},
new Employee() {
empName = "Manoj",
empAddress = "MUM",
StudentMarks = new Marks[] {
new Marks { ENG=59, MATHS = 40},
new Marks { ENG=60, MATHS = 80},
new Marks { ENG=80, MATHS = 10},
new Marks { MATHS = 90},
new Marks { ENG=70},}
},
};
foreach (var student in objs)
{
Console.WriteLine("{0} - {1}", student.empName, student.empAddress);
foreach (var mark in student.StudentMarks)
{
Console.WriteLine("Eng:{0}, Maths:{1}", mark.ENG, mark.MATHS);
}
}
}
public class Marks
{
public int ENG { get; set; }
public int MATHS { get; set; }
}
public class Student
{
public string empName { set; get; }
public string empAddress { set; get; }
public Marks[] StudentMarks { set; get; }
}
public class Employee : Student
{ }
However, that is not a good model. A better model would be like this:
void Main()
{
var objs = new List<Student>()
{
new Student() {
Name = "Manish",
Address = "MUM",
StudentMarks = new List<Mark> {
new Mark {Name="Eng", Score = 50},
new Mark {Name="Maths", Score = 60},
}
},
new Student() {
Name = "Manoj",
Address = "MUM",
StudentMarks = new List<Mark> {
new Mark {Name="Maths", Score = 100},
new Mark {Name="Eng", Score = 80},
new Mark {Name="Eng", Score = 70},
new Mark {Name="Eng", Score = 90},
new Mark {Name="Maths", Score = 90},
}
},
};
foreach (var student in objs)
{
Console.WriteLine("{0} - {1}", student.Name, student.Address);
foreach (var mark in student.StudentMarks)
{
Console.WriteLine("Name:{0}, Score:{1}", mark.Name, mark.Score);
}
}
}
public class Mark
{
public string Name { get; set; }
public int Score { get; set; }
}
public class Student
{
public string Name { set; get; }
public string Address { set; get; }
public List<Mark> StudentMarks { set; get; }
}
This one is still not very good. A better one might be:
void Main()
{
var objs = new List<Student>()
{
new Student() {
Name = "Manish",
Address = "MUM",
StudentMarks = new Dictionary<string,List<int>> {
{"Maths", new List<int> {60,70,50}},
{"Eng", new List<int> {80,70,90}},
}
},
new Student() {
Name = "Manoj",
Address = "MUM",
StudentMarks = new Dictionary<string,List<int>> {
{"Maths", new List<int> {70,90}},
{"Eng", new List<int> {40,50,60,60}},
}
},
};
foreach (var student in objs)
{
Console.WriteLine("{0} - {1}", student.Name, student.Address);
foreach (var course in student.StudentMarks)
{
Console.WriteLine("Course:{0}, Average:{1}", course.Key, course.Value.Average());
foreach (var score in course.Value)
{
Console.WriteLine(score);
}
}
}
}
public class Mark
{
public string Name { get; set; }
public int Score { get; set; }
}
public class Student
{
public string Name { set; get; }
public string Address { set; get; }
public Dictionary<string,List<int>> StudentMarks { set; get; }
}
And there are better ones.
Related
I am having problem adding to the model. I want to have a list in the Root1 or in the Viewmodel like fullname and authors list.
newModel1
AuthorsAccepted count=4
FullName = Jack
AuthorsAccepted count=4
FullName = Time Dean
With the code below it only adds the last one to the Root1. I created a a third model. Is there a way I can add to the third model both the lists or in Root1.
var fullName = "Jack";
var fullName2 = "Tim Dean";
var authors1 = new List<AuthorsAccepted1>() {
new AuthorsAccepted1(){ id = 1, Name="Bill"},
new AuthorsAccepted1(){ id = 2, Name="Steve"},
new AuthorsAccepted1(){ id = 3, Name="jon"},
new AuthorsAccepted1(){ id = 4, Name="nick"}
};
var authors2 = new List<AuthorsAccepted1>() {
new AuthorsAccepted1(){ id = 1, Name="jack"},
new AuthorsAccepted1(){ id = 2, Name="tim"},
new AuthorsAccepted1(){ id = 3, Name="james"},
new AuthorsAccepted1(){ id = 4, Name="mary"}
};
var newModel1 = new Root1();
newModel1.FullName = fullName;
newModel1.AuthorsAccepted = authors1;
var newModel2 = new Root1();
newModel2.FullName = fullName2;
newModel2.AuthorsAccepted = authors2;
}
}
public class Root1
{
public string FullName { get; set; }
public List<AuthorsAccepted1> AuthorsAccepted { get; set; }
}
public class AuthorsAccepted1
{
public string Name { get; set; }
public int id { get; set; }
}
public class Viewmodel
{
public Root1 AllModel { get; set; }
}
I have a list that basically look like this...
public class Area
{
public int Id { get; set; }
public string Name { get; set; }
public List<ZipCodeAdresses> ListOfIncludedDestinations { get; set; }
}
public class ZipCodeAdresses
{
public int AreaId { get; set; }
public List<Person> AdressList { get; set; }
}
public class Person
{
public string MottagarNamn { get; set; }
public string Street { get; set; }
}
var intListToRemove = new List<int>(){2,3};
var list = new List<Area>();
var subList = new List<ZipCodeAdresses>();
var personList = new List<Person>
{
new Person() {MottagarNamn = "User 1"},
new Person() {MottagarNamn = "User 2"}
};
subList.Add(new ZipCodeAdresses(){AdressList = personList , AreaId = 1});
personList = new List<Person>
{
new Person() {MottagarNamn = "User 3"},
new Person() {MottagarNamn = "User 4"}
};
subList.Add(new ZipCodeAdresses() { AdressList = personList, AreaId = 2 });
list.Add(new Area(){Name = "List A", ListOfIncludedDestinations = subList});
subList = new List<ZipCodeAdresses>();
personList = new List<Person>
{
new Person() {MottagarNamn = "User 5"},
new Person() {MottagarNamn = "User 6"}
};
subList.Add(new ZipCodeAdresses() { AdressList = personList, AreaId = 3 });
personList = new List<Person>
{
new Person() {MottagarNamn = "User 7"},
new Person() {MottagarNamn = "User 8"}
};
subList.Add(new ZipCodeAdresses() { AdressList = personList, AreaId = 4 });
list.Add(new Area() { Name = "List B", ListOfIncludedDestinations = subList });
I need to be able to remove from the list ListOfIncludedDestinations where AreaId is equal to any integer in intListToRemove which in this example is 2 and 3?
List<T> contains a method RemoveAll, that removes all entries that fulfill a certain condition. In your case it is:
foreach(var entry in list)
{
entry.ListOfIncludedDestinations.RemoveAll(x => intListToRemove.Contains(x.AreaId));
}
This loops through your list, and for every entry it removes all entries in ListOfIncludedDestinations that have an AreadId which is in intListToRemove.
Online demo: https://dotnetfiddle.net/ialnPb
You should add this sample code to remove them from the list :
foreach (var i in list)
i.ListOfIncludedDestinations.RemoveAll(o => intListToRemove.Contains(o.AreaId));
While working with Linq on Grouping sets, I found a problem while the query is returning the list.
My Code is written in LinqPad
public static void Main()
{
List<RequestEntry> RequestEntries = new List<RequestEntry>(){
new RequestEntry{
RequestId = 1,
RequestedDate = new DateTime(2018, 06, 01),
ApproverUserId = "STEVES",
ApproverUserName = "Steve Smith",
AuthorizerUserId = "JAMESS",
AuthorizerUserName = "James Sutherland"
},
new RequestEntry{
RequestId = 1,
RequestedDate = new DateTime(2018, 06, 01),
ApproverUserId = "GRAHAMS",
ApproverUserName = "Graham Smith",
AuthorizerUserId = "THABANGM",
AuthorizerUserName = "Thabang Moroe"
},
new RequestEntry{
RequestId = 2,
RequestedDate = new DateTime(2018, 06, 02),
ApproverUserId = "STEVES",
ApproverUserName = "Steve Smith",
AuthorizerUserId = "JAMESS",
AuthorizerUserName = "James Sutherland"
},
new RequestEntry{
RequestId = 3,
RequestedDate = new DateTime(2018, 06, 03),
ApproverUserId = "ROBINS",
ApproverUserName = "Robin Smith",
AuthorizerUserId = "TOMH",
AuthorizerUserName = "Tom Harrision"
},
new RequestEntry{
RequestId = 3,
RequestedDate = new DateTime(2018, 06, 03),
ApproverUserId = "CHRISS",
ApproverUserName = "Chris Smith",
AuthorizerUserId = "TOMH",
AuthorizerUserName = "Tom Harrision"
},
new RequestEntry{
RequestId = 3,
RequestedDate = new DateTime(2018, 06, 03),
ApproverUserId = "LIAMS",
ApproverUserName = "Liam Smith",
AuthorizerUserId = "TOMH",
AuthorizerUserName = "Tom Harrision"
}
};
var results = (
from r in RequestEntries
group r by new
{
r.RequestId, r.RequestedDate
} into g
select new RequestWithApprover(){
RequestId = g.Key.RequestId,
RequestedDate = g.Key.RequestedDate,
ApproverUserIds = g.Select(c => c.ApproverUserId).ToList(),
RequestApprovers = g.Select(c => new RequestApprover(){
ApproverUserName = c.ApproverUserName,
ApproverUserId = c.ApproverUserId
}).ToList(),
RequestAuthorizers = g.Select(c => new RequestAuthorizer(){
AuthorizerUserName = c.AuthorizerUserName,
AuthorizerUserId = c.AuthorizerUserId
}).ToList()
}).ToList();
results.Dump();
}
public class RequestEntry
{
public int RequestId
{
get;
set;
}
public string ApproverUserId
{
get;
set;
}
public string ApproverUserName
{
get;
set;
}
public string AuthorizerUserId
{
get;
set;
}
public string AuthorizerUserName
{
get;
set;
}
public DateTime RequestedDate
{
get;
set;
}
}
public class RequestApprover
{
public string ApproverUserId
{
get;
set;
}
public string ApproverUserName
{
get;
set;
}
}
public class RequestAuthorizer
{
public string AuthorizerUserId
{
get;
set;
}
public string AuthorizerUserName
{
get;
set;
}
}
public class RequestWithApprover
{
public int RequestId
{
get;
set;
}
public DateTime RequestedDate
{
get;
set;
}
public List<string> ApproverUserIds
{
get;
set;
}
public List<RequestApprover> RequestApprovers
{
get;
set;
}
public List<RequestAuthorizer> RequestAuthorizers
{
get;
set;
}
}
What I found is, For RequestID =3, the Number of RequestAuthorizer items should be 1, but it had multiplied. I found that in RequestID =3 the Maximum items in any list is 3, the RequestAuthorizer to multiplies to that Number. Refer below snapshot highlighted of the result:
Technically, In RequestId=3 the List<RequestAuthorizer> should only contain the Item of Tom Harrision and the count should be one.
How could I correct it?
How about grouping the RequestAuthorizer?
var results = (
from r in RequestEntries
group r by new
{
r.RequestId, r.RequestedDate
} into g
select new RequestWithApprover(){
RequestId = g.Key.RequestId,
RequestedDate = g.Key.RequestedDate,
ApproverUserIds = g.Select(c => c.ApproverUserId).ToList(),
RequestApprovers = g.Select(c => new RequestApprover(){
ApproverUserName = c.ApproverUserName,
ApproverUserId = c.ApproverUserId
}).ToList(),
RequestAuthorizers = g.
GroupBy(g1 => new {
g1.AuthorizerUserName,
g1.AuthorizerUserId }).
Select(c => new RequestAuthorizer(){
AuthorizerUserName = c.Key.AuthorizerUserName,
AuthorizerUserId = c.Key.AuthorizerUserId
}).ToList()
}).ToList();
When using LiteCollection's instance method Find(), I passed a predicate into it as a parameter. This is the Find() method: stus.Find(s => s.Birthday.Year <= 1996);, and full code here:
Student.cs:
public class Student
{
public ObjectId ID { get; set; }
public string Name { get; set; }
public int Grade { get; set; }
public DateTime Birthday { get; set; }
}
Program.cs:
class Program
{
static void Main(string[] args)
{
using (var db = new LiteDatabase("test.db"))
{
LiteCollection<Student> stus = db.GetCollection<Student>("students");
List<Student> studentList = new List<Student>()
{
new Student() {Name = "Nguyen Hoang Nguyen", Birthday = new DateTime(1997, 6, 3), Grade = 8},
new Student() {Name = "Nguyen Anh Tuan", Birthday = new DateTime(1997, 7, 12), Grade = 8},
new Student() {Name = "Pham Van Hung", Birthday = new DateTime(1996, 3, 26), Grade = 9}
};
stus.Insert(studentList);
var filteredStudents = stus.Find(s => s.Birthday.Year <= 1996);
Console.WriteLine("Student who has birth year before 1996:");
foreach (Student filteredStudent in filteredStudents)
{
Console.WriteLine($"- {filteredStudent.Name}, grade: {filteredStudent.Grade}");
}
Console.ReadKey();
stus.Delete(_ => true);
}
}
}
But filteredStudents doesn't contain any student, only "Student who has birth year before 1996:" line was printed to the console. So is anything here wrong or I missed something here? Thank you for reading my question.
You can not do that in liteDB
You must create a DateTime Object
var d = new DateTime(1996);
var filteredStudents = stus.Find(s => s.Birthday.Year <= d);
When I try to fill seed in my database I got an error:
The entity found was of type PetsOwner_FF460419E950312585B2229EA7AA6AEAA692190083B7EA5830FB08DEEE79E35D when an entity of type Caretaker was requested."
I went through all tips which I found, I examined the relation between classes and didn't find any solution.
context.CareTakers.AddOrUpdate(c => c.Id,
new Caretaker { Id = 1, Name = "Jan", Surname = "Nowak", Pesel = "29032400352", BankAccountNumber = "05 8202 1016 5582 6188 2074 1719", PhoneNumber = "666-449-292", Accomodations = new List<Accommodation> { context.Accomodations.Find(1) } },
new Caretaker { Id = 2, Name = "Anna", Surname = "Kowalski", Pesel = "29010209388", BankAccountNumber = "76 1610 1335 0163 2600 0322 4138", PhoneNumber = "789-335-709", Accomodations = new List<Accommodation> { context.Accomodations.Find(2) } },
new Caretaker { Id = 3, Name = "Sylwia", Surname = "Boski", Pesel = "42022605090", BankAccountNumber = "21 8499 0008 3163 0216 0506 9685", PhoneNumber = "538 -887-645", Accomodations = new List<Accommodation> { context.Accomodations.Find(3) } },
new Caretaker { Id = 4, Name = "Zbigniew", Surname = "Ryba", Pesel = "10262717572", BankAccountNumber = "61 9614 0008 9550 1997 2431 0213", PhoneNumber = "795-200-495", Accomodations = new List<Accommodation> { context.Accomodations.Find(4) } }
);
context.Accomodations.AddOrUpdate(a => a.Id,
new Accommodation { Id = 1, Beginning = new DateTime(2016, 1, 2), End = new DateTime(2016, 1, 3), PricePerDay = 10.5, Pet = context.Pets.Find(1), Caretaker = context.CareTakers.Find(1) },
new Accommodation { Id = 2, Beginning = new DateTime(2015, 2, 3), End = new DateTime(2016, 2, 4), PricePerDay = 100.0, Pet = context.Pets.Find(2), Caretaker = context.CareTakers.Find(2) },
new Accommodation { Id = 3, Beginning = new DateTime(2014, 9, 10), End = new DateTime(2016, 9, 11), PricePerDay = 12.3, Pet = context.Pets.Find(3), Caretaker = context.CareTakers.Find(3) },
new Accommodation { Id = 4, Beginning = new DateTime(2013, 11, 12), End = new DateTime(2016, 12, 13), PricePerDay = 41.4, Pet = context.Pets.Find(4), Caretaker = context.CareTakers.Find(4) }
);
My classes look like:
public class Accommodation {
public int Id { get; set; }//{BAG}
public virtual Caretaker Caretaker { get; set; }
public virtual Pet Pet { get; set; }
public double PricePerDay { get; set; }
public DateTime Beginning { get; set; }
public DateTime End { get; set; }
public double TotalPrice {
get {
return (End - Beginning).Days * PricePerDay;
}
}
}
[Table("Caretakers")]
public class Caretaker : Person {
public static int Percent => 20;//C# 6
public DateTime ObtainedLicense { get; set; }
public DateTime Hired { get; set; }
public double Salary => 0;
public virtual ICollection<Surgery> Surgeries { get; set; }
public virtual Vet Superior { get; set; }//rekurencyjna
//pochodny
public int AnimalsCurrentlyCareTaken => 0;//Lambda do wyliczenia ilosci opiekowaniymi sie zwierzetyami
public virtual ICollection<Accommodation> Accomodations { get; set; }
}