I need some help with an extraction method. I am currently working on a school card project and I need to extract all students learning in certain teachers class. I have table named Teachers, table named Classes and a table named Students. I want by given Teacher ID to receive all the students in his/her class.
Table Teachers contains TeacherID, Name, and so on.
Classes contains TeacherID, StudentID.
Students contains StudentID and FirstName, LastName.
My problem is that I want to return a whole list of students and I am not quite sure how to do it and what type I should be using. Could you please assist me ?
The code so far
var studentID = from t in context.Teachers
join s in context.Classes on t.TeacherID equals s.TeacherID
join stu in context.Students on s.StudentID equals stu.StudentID
select stu.FirstName
.SelectMany();
As you can see the use of .SelectMany() is wrong and thus I do not seem to be able to replace it with anything so far. Also, since there are many names the use of var is incorrect, I suppose.
how about this..
var studentID = from t in context.Teachers
join s in context.Classes on t.TeacherID equals s.TeacherID
join stu in context.Students on s.StudentID equals stu.StudentID
WHERE stu.FirstName == FirstName
select t;
I would do it on something more unique what if you have 5 Thomas's ?
Related
Let's suppose there is a database to manage students.
I'm using the following (simplified) classes:
Student
int id
string name
StudentEnrollmentInfo
int studentId
int courseId
Course
int courseInfo
string name
In the aforementioned tables, studentenrollmentinfo is a joining table, to handle the many to many relation between courses and students.
Now I'd like to query the names of all students who are currently enrolled in a list of different courses. For example:
Get all students enrolled in Biology, Maths and Arts, I'd like to get all students that are enrolled in ALL of those classes.
I have tried the following:
List<string> requiredCourses = new List<string>(){"Maths", "Biology", "Arts"};
var query = from student in _context.students
join enrollmentInfo in _context.StudentEnrollmentInfo on
student.id equals enrollmentInfo.studentId
join course in _context.Course on course.id equals enrollmentInfo.courseId
where requiredCourses.All(r => r.equals(course.name))
select(student.name)
This however does not work, my guess is because the joins, in addition to the joining table flattens the list, making it where every student with every course combination is a unique value, making it so that there is never any AND condition that is true.
Can anyone suggest something to point me in the right direction?
List requiredCourses = new List() { "Maths", "Biology", "Arts" };
var query = (from student in _context.students
join enrollmentInfo in _context.StudentEnrollmentInfo on student.id equals enrollmentInfo.studentId
join course in _context.Course on course.id equals enrollmentInfo.courseId
where **requiredCourses.Contains(course.name)**
select student.name).Distinct().ToList()
hope it will solve your purpose. distinct will help for your unique data
I have 2 classes for examle:StudentClass and SelectedLessonClass.
StudentClass{
studentId,
name,
family}
SelectedLessonClass{
studentId,
lessonId}
I need information of students that select lesson with lessonId=12;
I use join command:
students=students.join(selectedLessons.where(sl=>sl.lessonId==12).tolist(),st=>st.studentId,sl=>sl.studentId,.....)
Please Guide me,what things must I fill instead of ..... ?
Thanks
Let say you have listStudents and listLessons you can try something like this (result is the list of students that match your criteria):
var result = from s in listStudents
join l in listLessons
on s.studentId equals l.studentId
where l.lessonId=12
select s;
I'm brand new to .net MVC, and while I have some basic experience with writing SQL queries, I'm not sure how to go about what I need to do for .NET.
My initial query looks like this:
var results = (from s in db.Members
join sa in db.FocusArea on s.ID equals sa.MemberID
where sa.Area == SearchString
select new { s.ID, s.Name, s.Overview }).ToList();
This is not functioning correctly. It is seaching in the s.Overview for some reason. And, I need to make this query much more complicated. In short, I have three tables I need to search across. And with this query, it is not working:
var conceptResults = (from s in db.Cohorts
join oa in db.OutcomeArea on s.ID equals oa.CohortID
where ((oa.Area.Contains(SearchString))
|| (oa.OutcomeType.Contains(SearchString)))
select new { s.ID, s.Name, s.Overview }).ToList();
I need to use my SearchString to search for matches in both Area and Description in db.FocusArea.
I also need to use my SearchString to search for matches (contains) in another table db.SpecificFocusAreas for column SFocusArea where again the join is the ID/MemberID.
Is there a way to essentially do a join or join type of statement? I don't want to join all three tables because I am looking for results from either of the two joins, not from all joins.
I have an recruiting database that looks like this: Recruiter has many Students. Students have many "contacts" (how many times we have called/emailed etc.)
I want a recruiter to be able to see when the last time each student of theirs was contacted. I'm developing this app in c# and my database is mySQL.
My table names are employee, students, contact_his and the fields I want to join, given one employeeid are employee.idemployee + students.employee_id then join all of those to contact_his.students_id + students.idstudents. But I have no idea how joins work.. My current code looks like this, but it does not like it:
"SELECT students.* FROM admissions.students
WHERE students.employee_id='PASS VARIABLE HERE'
JOIN contact_his ON contact_his.students_id = s.idstudents
WHERE c.date = (SELECT MAX(date);"
SELECT s.*, MAX(h.date) last_contact
FROM students s
JOIN contact_his h ON h.students_id = s.idstudents
WHERE s.employee_id = 'PASS VARIABLE HERE'
GROUP BY s.idstudents
I have two Tables - tblExpenses and tblCategories as follows
tblExpenses
ID (PK),
Place,
DateSpent,
CategoryID (FK)
tblCategory
ID (PK),
Name
I tried various LINQ approaches to get all distinct records from the above two tables but not with much success. I tried using UNION and DISTINCT but it didnt work.
The above two tables are defined in my Model section of my project which in turn will create tables in SQLite. I need to retrieve all the distinct records from both the tables to display values in gridview.
Kindly provide me some inputs to accomplish this task. I did some research to find answer to this question but nothing seemed close to what I wanted. Excuse me if I duplicated this question.
Here is the UNION, DISTINCT approaches I tried:
DISTINCT # ==> Gives me Repetitive values
(from exp in db.Table<tblExpenses >()
from cat in db.Table<tblCategory>()
select new { exp.Id, exp.CategoryId, exp.DateSpent, exp.Expense, exp.Place, cat.Name }).Distinct();
UNION # ==> Got an error while using UNION
I think union already does the distict when you join the two tables you can try somethin like
var query=(from c in db.tblExpenses select c).Concat(from c in
db.tblCategory select c).Distinct().ToList();
You will always get DISTINCT records, since you are selecting the tblExpenses.ID too. (Unless there are multiple categories with the same ID. But that of course would be really, really bad design.)
Remember, when making a JOIN in LINQ, both field names and data types should be the same. Is the field tblExpenses.CategoryID a nullable field?
If so, try this JOIN:
db.Table<tblExpenses>()
.Join(db.Table<tblCategory>(),
exp => new { exp.CategoryId },
cat => new { CategoryId = (int?)cat.ID },
(exp, cat) => new {
exp.Id,
exp.CategoryId,
exp.DateSpent,
exp.Expense,
exp.Place,
cat.Name
})
.Select(j => new {
j.Id,
j.CategoryId,
j.DateSpent,
j.Expense,
j.Place,
j.Name
});
You can try this queries:
A SELECT DISTINCT query like this:
SELECT DISTINCT Name FROM tblCategory INNER JOIN tblExpenses ON tblCategory.categoryID = tblExpenses.categoryID;
limits the results to unique values in the output field. The query results are not updateable.
or
A SELECT DISTINCTROW query like this:
SELECT DISTINCTROW Name FROM tblCategory INNER JOIN tblExpenses ON tblCategory.categoryID = tblExpenses.categoryID;<br/><br/>
looks at the entire underlying tables, not just the output fields, to find unique rows.
reference:http://www.fmsinc.com/microsoftaccess/query/distinct_vs_distinctrow/unique_values_records.asp