Below I have the constructors of two classes. Student and Course
I am trying to compare the value of _completedCourses value from the Student class against the coursecode of the Course class.
_completedCourses is a dictionary of Courses and their grades. I am trying to find which course is not completed and print that list.
Am I going about this correct.
public Course(string coursName, string courseCode, char passingGrade, double noOfCredits, Semester semesterOfferd, int major)
{
this.CourseName = coursName;
this.CourseCode = courseCode;
this.PassingGrade = passingGrade;
this.NoOfCredits = noOfCredits;
this.SemesterOfferd = semesterOfferd;
this.prerequisiteCourses = new List<Course>();
this._enrolledStudents = new List<Student>();
this._major = major;
}
public Student(int studentID, string studentName, Status studentStatus, StudentMajor studentMajor)
{
this._studentID = studentID;
this._studentName = studentName;
this._studentStatus = studentStatus;
this._studentMajor = studentMajor;
this._course = new Course();
this._completedCourses = new Dictionary<string, char>();
countStudent++;
}
public void RemainingCourses()
{
var cKey = _completedCourses.ContainsKey(_course.CourseCode);
Console.WriteLine("Remaining Courses");
if (cKey.Equals(_course.CourseCode))
{
foreach (KeyValuePair<string, char> count in _completedCourses)
{
{
{
Console.WriteLine(count.Key + " " + count.Value);
// Console.WriteLine("Course " + count.Value);
count.ToString();
}
}
}
}
}
UPDATE!!!
The following line of code in my driver class achieves what I want
Console.WriteLine("Enter Student ID ");
input = Convert.ToInt32(Console.ReadLine());
Console.Clear();
if (input.Equals(id.StudentID)) {
id.DisplayCompletedCourse();
foreach (var sub in isdCourses) {
var cKey = id.CompletedCourses.ContainsKey(sub.CourseCode);
if (!cKey)
{
Console.WriteLine(sub.CourseCode);
}
}
}
The ContainsKey method returns a boolean value (true/false), so in your if condition:
if (cKey.Equals(_course.CourseCode))
cKey is a boolean but _course.CourseCode is a string, so this will never be true, thus this will never go within the if block.
Try to rewrite it like this:
public void RemainingCourses()
{
var cKey = _completedCourses.ContainsKey(_course.CourseCode);
Console.WriteLine("Remaining Courses");
if (cKey) // if cKey is "true", then the dictionary contains the CourseCode
{
foreach (KeyValuePair<string, char> count in _completedCourses)
{
Console.WriteLine(count.Key + " " + count.Value);
}
}
}
As a side note, please beware of the formatting, you are adding unnecessary curly braces. Also, this could be simplified with linq, but I kept it as close as possible to the original code.
Related
I have these values in my listbox (above the left listbox you see the headers of the table, but this is in Dutch):
In the listbox on the right you see: employeeid, questionid and score. In the listbox on the right I want the total average score of every employeeid, by a press on a button. I need to make a algorithm, that takes the correct values of the listbox.
How can I do this? I don't know how I can say that I only want certain values from the listbox (employeeid and score, and not questionid).
I'm using a class to load in the data:
public List<ScoreMdw> GetScoreMdwList()
{
List<ScoreMdw> scoremdwList = new List<ScoreMdw>();
conn.Open();
string query = ("Select employeeid, questionid, score from contentment");
SqlCommand cmd = new SqlCommand(query, conn);
try
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
ScoreMdw sm = new ScoreMdw((int)dr["employeeid"], (int)dr["questionid"], (int)dr["score"]);
scoremdwList.Add(sm);
}
}
}
catch (Exception ex)
{
Exception error = new Exception("error", ex);
throw error;
}
finally
{
conn.Close();
}
return scoremdwList;
}
In the while loop I'm using an other class:
class ScoreMdw
{
private int employeeid;
private int questionid;
private int score;
public ScoreMdw(int nr, int id, int s)
{
this.employeeid= nr;
this.questionid= id;
this.score = s;
}
public int EmployeeId
{
get { return employeeid; }
}
public int QuestionId
{
get { return questionid; }
}
public int Score
{
get { return score; }
}
public override string ToString()
{
string s = string.Format("{0} \t{1} \t{2}", this.employeeid, this.questionid, this.score);
return s;
}
}
In my main window I'm doing this:
private void btnLoadScores_Click(object sender, RoutedEventArgs e)
{
scoremdwList = new List<ScoreMdw>();
try
{
conn.Open();
List<string> headers = so.GetContentmentHeaders();
foreach (string header in headers)
txtHeader.Text += header + "\t";
scoremdwList = so.GetScoreMdwList();
lbScores.ItemsSource = scoremdwList;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();
}
}
You need a linq query to sum up score of same employee id like
lbScores.ItemsSource = (from e in scoremdwList
group e by e.EmployeeId into grp
select new
{
EmployeeId = grp.Key,
TotalScore = grp.Sum(a => a.Score)
}).ToList();
Where EmployeeId and TotalScore are the columns of target listbox
// string: employeeid - first int: total score - second int: number of questions
Dictionary<string, Tuple<int, int>> results = new Dictionary<string, Tuple<int, int>>();
foreach (ListViewItem item in lstvwSource.Items)
{
// check if employeeid is in Dictionary
if (results.ContainsKey(item.Text))
{
// if employeeid exists in dictionary
// add currnet score to total score
// and add one to number of questions
results[item.Text] = new Tuple<int, int>(Convert.ToInt32(item.SubItems[1].Text) + results[item.Text].Item1, +results[item.Text].Item2 + 1);
}
else
{
// if employeeid does not exist in dictionary
// add employeeid , score of the question
// set number of questions to 1
Tuple<int, int> tuple = new Tuple<int, int>(Convert.ToInt32(item.SubItems[1].Text), 1);
results.Add(item.Text, tuple);
}
}
//
lstvwDest.Items.Clear();
foreach (var result in results)
{
ListViewItem newItem = new ListViewItem();
newItem.Text = result.Key; // employeeid
newItem.SubItems.Add(result.Value.Item1.ToString()); // total score
double avg = (double)result.Value.Item1 / (double)result.Value.Item2;
newItem.SubItems.Add(avg.ToString()); // average score
lstvwDest.Items.Add(newItem);
}
Question is how can I get only the values from my dictionary that contains one key but has a list of values assigned to it. I want to be able to grab all of the values and convert them into a tuple. Any help would be greatly appreciated.
Main code
private static Dictionary<string, List<string>> v_dict_info = new Dictionary<string, List<string>>();
public static Dictionary<string, List<string>> V_dict_info
{
get => v_dict_info;
set => v_dict_info = value;
}
public static string Vista_con_s
{
get => _vista_con_s;
set => _vista_con_s = value;
}
public void get_emp_info_addr()
{
string info_addr = "select " +
"c.empno " +
"from " +
"[V4].[dbo].[person] c ";
try
{
vista_conn = new SqlConnection();
vista_conn.ConnectionString = _vista_con_s;
vista_command = new SqlCommand(info_addr, vista_conn);
vista_conn.Open();
vista_reader = vista_command.ExecuteReader();
if (vista_reader.HasRows)
{
while (vista_reader.Read())
{
v_dict_info.Vista_addr_list("empno", vista_reader.GetValue(0).ToString());
}
}
}
catch (SqlException sq_x)
{
System.Diagnostics.Debug.WriteLine("Error connecting to the database! -Error Msg: {0}", sq_x.ToString());
}
var count = v_dict_info["empno"].Count;
for (int i = 0; i < count; i++)
{
var empno = v_dict_info["empno"][i];
Console.WriteLine("{0}", empno);
}
// how can I get only the values associated to "empno" key
var xx = empno //this only return the first element :( I want all of them
Console.WriteLine(xx.ToString());
}
Here is my method for adding multiple values to one key in a dictionary.
public static class Multi_dict
{
public static void Vista_addr_list<T, U>(this IDictionary<T, List<U>> dict, T key, U elementToList)
{
List<U> list;
bool exists = dict.TryGetValue(key, out list);
if (exists)
{
dict[key].Add(elementToList);
}
else
{
dict[key] = new List<U>();
dict[key].Add(elementToList);
}
}
Presuming this is really necessary (and I agree with Gusman's comment on the question - I don't think this is really what you need), you're already half-way there.
Access the list via the key, and then select out your tuple:
dict[key].Select(x => new Tuple<string, string>(key, x));
You can find a working fiddle here: https://dotnetfiddle.net/jPX0bU
Major Edit: I am doing a bad job of explaining :(
I have two classes:
public class UserDefinitions// a list of 'Items', each'group of items belong to a user. I handle User logic elsewhere, and it works flawlessly.
{
public List<Item> items { get; set; }
}
public class Item //the User definitions. A user could have 1 or 15 of these. They would all be a single 'line' from the CSV file.
{
public string definitionKey { get; set; }
public string defName { get; set; }
public string defValue { get; set; }
}
Which I wanna build with a CSV File. I build this CSV File, so I make it using the same parameters every time.
I run SQL on my company's DB to generate results like so: http://i.imgur.com/gS1UJot.png
Then I read the file like so:
class Program
{
static void Main(string[] args)
{
var userData = new UserDefinitions();
var csvList = new List<Item>();
string json = "";
string fPath = #"C:\test\csvTest.csv";
var lines = File.ReadAllLines(fPath);
Console.WriteLine(lines);
List<string> udata = new List<string>(lines);
foreach (var line in udata)
{
string[] userDataComplete = line.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);// this cleans any empty cells from the CSV
csvList.Add(new Item { definitionKey = userDataComplete[1], defName = userDataComplete[2], defValue = userDataComplete[3] });
}
json = JsonConvert.SerializeObject(csvList); //everything below is for debugging/tracking progress
Console.WriteLine(json);
Console.ReadKey();
StreamWriter sw = new StreamWriter("C:\\test\\testjson.txt");
sw.WriteLine(json);
sw.Close();
}
}
This ALMOST does what I want. The output json is from the first 'column' of the csv data
[{"definitionKey":"uuid1","defName":"HairColor","defValue":"Brown"},{"definitionKey":"uuid1","defName":"HairColor","defValue":"Blonde"},{"definitionKey":"uuid1","defName":"HairColor","defValue":"Blue"}]
When using the screen shot as an example, the wanted output should be
[{"attributeDefinitionKey":"uuid1","name":"HairColor","value":"Brown"},{"definitionKey":"uuid2","defName":"FreckleAmount","defValue":"50"}]
[{"attributeDefinitionKey":"uuid1","name":"HairColor","value":"Blonde"},{"definitionKey":"uuid2","defName":"FreckleAmount","defValue":"null"}]
[{"attributeDefinitionKey":"uuid1","name":"HairColor","value":"Blue"},{"definitionKey":"uuid3","defName":"Tattoos","defValue":"5"}]
I can't pick out certain aspects at will, or apply them to Items. For example there maybe 10 users or 5000 users, but the definitionKey will always be the [1], and adding '3' will get every subsequent defintionKey. Just like the defName will always be in the [2] spot and adding 3 will get every subsequent defName if there are any, this is all per line.
I know I have to add some +3 logic, but not quite sure how to incorporate that. Maybe a for loop? a nested for loop after a foreach loop? I feel I am missing something obvious!
Thanks again for any help
This reads the csv line for line and converts each row to json, while adapting to the change in the amount of columns.
This only works if the CSV follows your rules:
one userId and
x amount of "Things" with 3 columns per "Thing".
private static void Main(string[] args)
{
var file = new StreamReader(#"C:\test\csvTest.csv");
string line;
var itemsJson = new List<string>();
file.ReadLine();
while ((line = file.ReadLine()) != null)
{
var sb = new StringBuilder();
var fields = line.Split(',');
sb.Append(GetKeyValueJson("UserId", fields[0]));
for (var i = 1; i < fields.Length; i += 3)
{
var x = (i + 3) / 3;
sb.Append(GetKeyValueJson($"Thing {i + x} ID", fields[i]));
sb.Append(GetKeyValueJson($"Thing {i + x} ID", fields[i + 1]));
sb.Append(i + 3 == fields.Length
? GetKeyValueJson($"Thing {i + x} ID", fields[i + 2], true)
: GetKeyValueJson($"Thing {i + x} ID", fields[i + 2]));
}
itemsJson.Add(WrapJson(sb.ToString()));
}
var json = WrapItems(itemsJson);
Console.ReadLine();
}
private static string GetKeyValueJson(string id, string value, bool lastPair = false)
{
var sb = new StringBuilder();
sb.Append('"');
sb.Append(id);
sb.Append('"');
sb.Append(':');
sb.Append('"');
sb.Append(value);
sb.Append('"');
if (!lastPair)
sb.Append(',');
return sb.ToString();
}
private static string WrapJson(string s)
{
var sb = new StringBuilder();
sb.Append('{');
sb.Append(s);
sb.Append('}');
return sb.ToString();
}
private static string WrapItems(List<string> jsonList)
{
var sb = new StringBuilder();
sb.Append('"');
sb.Append("Items");
sb.Append('"');
sb.Append(':');
sb.Append('[');
sb.Append(jsonList.Aggregate((current, next) => current + "," + next));
sb.Append(']');
return WrapJson(sb.ToString());
}
}
It's not pretty and sorting would be tough, but it should adapt to the column amount as long as it is in 3's.
I got a ID
which are LSHOE-UCT. How can I substring and seperate those ID in order to become:
gender = "L"
Product = "Shoe"
Category = "UCT"
Here is my code:
private void assignProductCategory(string AcStockCategoryID)
{
//What should I insert?
string[] splitParameter = AcStockCategoryID.Split('-');
}
I need to seperate those, ID them and insert to difference table from my database. And that is where I am having the main problem
string[] s = AcStockCategoryID.Split('-');
string gender = s[0].Substring(0, 1);
string Product= s[0].Substring(1, s[0].Length - 1);
string Category = s[1];
To try a different approach, this would work, too.
string id = "LSHOE-UCT";
string gender = id.Substring(0,1);
int indexOfDash = id.IndexOf("-");
string product = id.Substring(1, indexOfDash - 1);
string category = id.Substring(indexOfDash + 1);
try this, I just typed it randomly apologies if there are any typos...
string id = "LSHOE-UCT";
string[] arr = id.Split('-');
string gender = id.Substring(0,1); // this will give you L
string product = arr[0].Substring(1); // this will give you shoe
string category = arr[1]; // this will give you UCT;
Warning: Complete Overkill
You could also use LINQ's extension methods (IEnumerable) to accomplish this. I thought I'd have a little thought experiment about how you could use IEnumerable to over-engineer a solution:
int indexOfDash = id.IndexOf("-");
var firstPart = id.TakeWhile(s => s != '-');
var linqGender = firstPart.Take(1).ToArray()[0]; // string is L
var linqProduct = String.Join("", firstPart.Skip(1).Take(indexOfDash-1)); // string is SHOE
var secondPart = id.Skip(indexOfDash+1);
var linqCategory = String.Join("", secondPart); //string is UCT
EDIT: Updated my answer due to the ID format not being correct in my first post.
If your acStockCategoryID is always going to be in the format of LSHOE-UTC, then you could do something like the following:
private void assignProductCategory(string AcStockCategoryID)
{
string[] splitParameter = AcStockCategoryID.Split('-');
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendLine("gender=" + splitParameter[0].Substring(0, 1));
sb.AppendLine("Product=" + splitParameter[0].Substring(1));
sb.AppendLine("Category=" + splitParameter[1]);
// use sb.ToString() wherever you need the results
}
I would do it backwards.
public class LCU
{
public string Gender {get; set;}
public string Product {get; set;}
public string Category {get; set;}
public LCU(){}
}
private static LSU LShoe_UctHandler(string id)
{
var lcu = new LCU();
var s = id.Split('-');
if (s.length < 2) throw new ArgumentException("id");
lcu.Category = s[1];
lcu.Gender = s[0].Substring(0,1);
lcu.Product = s[0].Substring(1);
return lcu;
}
Then just pass ID to LShoe_UctHandler like so...
var lcu = LShoe_UctHandler("LGlobtrotters-TrainingShoes");
Console.WriteLine("gender = {0}", lcu.Gender);
Console.WriteLine("Product = {0}", lcu.Product );
Console.WriteLine("Category = {0}", lcu.Category );
[Hand keyed - so sorry for typos and casing errors]
Try this:
string id = "LSHOE-UCT";
Console.WriteLine("Gender: {0}",id.Substring(0,1));
Console.WriteLine("Product: {0}",id.Split('-')[0].Substring(1));
Console.WriteLine("Product: {0}", id.Split('-')[1]);
How loop sub classes properties from the main class?
public class GroupA
{
public string FullName = "", BirthDay = "";
}
public class GroupB
{
public string Email = "";
}
public class GroupC
{
public string Phone;
}
public class MainGroup
{
public GroupA GroupA;
public GroupB GroupB;
public GroupC GroupC;
}
protected void Page_Load(object sender, EventArgs e)
{
GroupA NewGroupA = new GroupA();
NewGroupA.FullName="TEST MASTER";
NewGroupA.BirthDay="02/20/1984";
GroupB NewGroupB = new GroupB();
NewGroupB.Email="noreply#test.com";
GroupC NewGroupC=new GroupC();
NewGroupC.Phone="555 123 4567";
//Assign new class instances to the main class
MainGroup NewMainGroup= new MainGroup();
NewMainGroup.GroupA=NewGroupA;
NewMainGroup.GroupB=NewGroupB;
NewMainGroup.GroupC=NewGroupC;
//Loop through the main class
foreach (var Group in typeof(MainGroup).GetFields())
{
Response.Write("<BR>MainGroupName= " + Group.Name + " Value= " + Group.GetValue(NewMainGroup).ToString());
//PROBLEM IS HERE. Can't loop through the subclass We need to display the GroupA, GroupB, GroupC below.
foreach (var SubGroup in Group)
{
Response.Write("<BR>");
}
}
}
If I understand your question right, I think this code is your answer:
foreach (var group in NewMainGroup.GetType().GetFields())
{
var groupInstance = group.GetValue(NewMainGroup);
foreach (var subGroup in groupInstance.GetType().GetFields())
{
Response.Write("<br />" + subGroup.Name + " = " + subGroup.GetValue(groupInstance));
}
}
What you should to is store a reference to the group-variable (not just the type). So, if you take your code and do something like this:
foreach (var GroupType in typeof(MainGroup).GetFields())
{
object Group = GroupType.GetValue(NewMainGroup);
Response.Write("<BR>MainGroupName= " + GroupType.Name + " Value= " + Group.ToString());
foreach(var SubGroupType in Group.GetType().GetFields()) {
object SubGroup = SubGroupType.GetValue(Group);
Response.Write("<BR>SubGroupName= " + SubGroupType.Name + " Value= " + SubGroup.ToString());
}
}
Haven't tested it, but I think that should about work. At least get you started.
Oh, and by the way, I think I have a better method for you, try this one:
public Dictionary<string, object> GetObjectFields(object obj) {
var dict = new Dictionary<string, object>();
var t = obj.GetType();
foreach(var f in t.GetFields()) {
dict[f.Name] = f.GetValue(obj);
}
return dict;
}
Then you can simply do this:
var groupVars = GetObjectFields(NewMainGroup);
foreach(var gv in groupVars) {
Response.Write(<BR>MainGroupName= " + gv.Key + " Value= " + gv.Value.ToString());
var subGroupVars = GetObjectFields(gv.Value);
foreach(var sgv in subGroupVars) {
Response.Write(<BR>SubGroupName= " + sgv.Key + " Value= " + sgv.Value.ToString());
}
}
First thing first GroupA, GroupB and GroupC are not subclasses of MainGroup.
You are using composition and have members which are of GroupA, GroupB and GroupC types.
Not sure why you want to relflect the types when you already know the type you have.
You could have used MainGroup.GroupA.Name and so on. May be the code you posted is just an example?
The below method should reflect the basic structure and suit your purpose.
static String Reflect(Object data)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (PropertyInfo propertyInfo in data.GetType().GetProperties())
{
if (propertyInfo.PropertyType.IsClass && propertyInfo.PropertyType != typeof(String))
{
stringBuilder.AppendFormat("{0} : {1} {2}", propertyInfo.Name, Environment.NewLine
, Reflect(propertyInfo.GetValue(data, null)));
}
else
{
stringBuilder.AppendFormat("{0} = {1}, {2}", propertyInfo.Name, propertyInfo.GetValue(data, null), Environment.NewLine);
}
}
return stringBuilder.ToString();
}
usage:
MainGroup main = new MainGroup
{
A = new GroupA { Name = "GroupA", ID = 1 },
B = new GroupB { Date = DateTime.UtcNow },
C = new GroupC { HasData = false }
};
Reflect(main);