The objective of this task is to run a LINQ query with Where
The objective of this task is to run a LINQ query with Where method for extracting the course objects contained in Courses list for all courses offered by DeptCode “MKTG”, and save the results in a sub list named MktgCourses. Do not assume that you already know the MKTG couses. You must run the appropriate LINQ query. Do not try to do it in some other ways.
After you have run the query and saved the result in another list (say, MktgCourses), enter the following code:
// Display the courses in the MktgCourses list:
Console.WriteLine(\"The courses in the MktgCourses are:\");
Now use a foreach loop to display the courses in the MktgCourses.
Here is what I have so far:
Course.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CGAsn1.cgClasses
{
class Course
{
int CourseID;
string CourseCode;
string Title;
int CreditHour;
string DeptCode;
public Course(int id, string code, string t, int h, string d)
{
CourseID = id;
CourseCode = code;
Title = t;
CreditHour = h;
DeptCode = d;
}
//Override the Tostring method
public override string ToString()
{
string CourseDesc =
String.Format(\"\\t{0} \\t{1} \\t{2} \\t{3} \\t{4}\", CourseID, CourseCode, Title, CreditHour, DeptCode);
return CourseDesc;
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CGAsn1.cgClasses;
namespace CGAsn1.cgClasses
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(\"Asn1 Part1 Task2: Testing the Course \ \");
Console.WriteLine(\"These courses are:\ \");
Course c1 = new Course(1, \"ACCT2000\", \"Intro to Accounting\", 4, \"ACCT\");
Console.WriteLine(c1.ToString());
{
Course c2 = new Course(2, \"ACCT4000\", \"Cost Accounting\", 5, \"ACCT\");
Console.WriteLine(c2.ToString());
//==============================================================
// Part 2 starts here
// ==========================================================
Console.WriteLine(\"================================================================================\");
Console.WriteLine(\"Part 2 starts here\ \");
List<Course> Courses = new List<Course>()
{
new Course(1, \"ACCT2000\", \"Intro to Accounting\", 4, \"ACCT\"),
new Course(2, \"ACCT4000\", \"Cost Accounting\", 5, \"ACCT\"),
new Course(3, \"INFS3000\", \"Intro to Programming\", 3, \"INFS\"),
new Course(4, \"INFS3700\", \"Database System\", 4, \"INFS\"),
new Course(5, \"INFS4010\", \"System Analysis\", 6, \"IFNS\"),
new Course(6, \"MKTG1000\", \"Intro to Marketing\", 3, \"MKTG\"),
new Course(7, \"MKTG2000\", \"Transportation Theory\", 4, \"MKTG\"),
};
Console.WriteLine(\"Part 2 Task2\");
Console.WriteLine(\"The courses in the List are:\");
foreach (Course h in Courses)
{
Console.WriteLine(h.ToString());
}
Console.WriteLine(\"================================================================================\");
Console.WriteLine(\"Part2 Task3 \");
Console.WriteLine(\"Create a sublist of courses for DeptCode MKTG from Courses:\");
//Run a \"LINQ Where\" query for DeptCode \"MKTG\" on Courses and store the result in a List object.
// Name the sublist as MktgCourse
List<Course> x = new List<Course>();
Console.WriteLine(\"The courses in MktgCourses are: \");
foreach ())
{
Console.WriteLine(h.ToString());
}
Console.ReadLine();
}
}
}
}
}
Solution
x.Add()
sublists the data
Using Add u can add data to x list after creating a list




