Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I just was wondering if I can perform a Query just like that:

What I have tried:

C#
List <studentclasslist> = _context.StudentClass
                    .Where(sc => sc.ClassId == 1)
                    .ToList();
 
List<int> studentIdList = new List<int>(); 

foreach (var student in studentClassList )
{
   studentIdList = _context.Student.Where(st => st.Id == student.StudentId).ToList(); 
}


Thanks in advance.
Posted
Updated 7-May-19 1:34am
v2

To me this kind of code ... that will obviously not compile ... indicates you have not really studied the basics of using the powerful tools that Linq provides.

Don't worry, though: everyone I know has gone through a learning curve to get somewhat fluent in using Linq :) Well, okay, imho, people with a deep background in DB query languages have an easier time; I just don't know them :)

Please consider getting a book on Linq, and using the on-line tool, LinqPad [^], provided by the Albihari brothers. They have several books out on Linq; and, you may find this useful: [^].

I have found my students respond well to the idea that the 'Where operator is a "selection function;" and, the 'Select operator is a function that enumerates and transforms elements into another form.
 
Share this answer
 
v2
Comments
Maciej Los 8-May-19 2:33am    
5ed!
I almost overlooked that...
Just to add, you need to include the Linq namespace for that stuff to start working, otherwise you'll have a List class but not those operators

List<int> studentIdList = new List<int>(); 


Also, this makes no sense. Set it to null if you need to set it inside a scope. Your code sets it over and over and does nothing with it? In fact, your code won't compile. Post real code when you ask a question....
 
Share this answer
 
v2
Comments
BillWoodruff 7-May-19 18:35pm    
+4 to counteract an undeserved down-vote
Christian Graus 7-May-19 18:39pm    
Cheers
Well, you could ... if you got the names right, C# is case sensitive ... but it wouldn't do much that was useful.

The assignment inside the loop means that the eventual result will be the final result only, because it "throws away" all previous results each time round the loop.

You might want to consider List.AddRange instead of an assignment, but at a guess you might want to think about your data and the relationships between them rather than leaping into code like that - even without knowing exactly what you are trying to do that looks like a very inefficient way to do things!
 
Share this answer
 
This piece of code is not clear to me:
C#
List<int> studentIdList = new List<int>();

foreach (var student in studentClassList )
{
studentIdList = _context.Student.Where(st => st.Id == student.StudentId).ToList();
}


If you would like to get list of students in a specific class, you should do something like this:
C#
var studentsinclass = _context.Student
    .Where(st => _context.StudentClass
        .Any(sc => sc.ClassId == 1 && st.Id == sc.StudentId))
    .ToList();


Good luck!
 
Share this answer
 
v2
Comments
BillWoodruff 7-May-19 18:34pm    
Hi Maciej, the use of 'Any in your last example will return a bool if any element matches ... I don't think that's what you really want here.

cheers, Bill
Maciej Los 8-May-19 2:08am    
Bill, take a look at example again. 'Any' method is used as a condition to 'Where'.
Anyway, thanks for your comment.
Cheers,
Maciej
BillWoodruff 8-May-19 3:01am    
Okay, I am re-calibrating ... thanks !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900