asp.net mvc - use linq in operator -
i want in operator in mvc linq.
like sql (stud_id int, primary key , auto increment):
select * student stud_id in (1,4,6,10,5);
how can adapt linq sql?
like
list<int> nl = new list<int>(); nl.add(1); nl.add(4); nl.add(6); nl.add(10); nl.add(5); list<student> students = db.student.where(a => a.stud_id.in(nl)).tolist(); //this code fitting mind :d
or other scenario
list<student> st = db.studentorderby(x => guid.newguid()).take(5); //only create auto list student type list<student> students = db.student.where( => a.stud_id.in(st.select(b => b.stud_id).tolist()) ).tolist(); //again fitting
i can this
list<student> students = new list<student>(); foreach(var item in nl) { students.add(db.student.where(a => a.stud_id == item).first()); }
but dont want use or foreach or do-while or while :d
try this:
list<student> students = db.student.where(a => nl.contains(a.stud_id)).tolist();
this checks, if student id present in list nl
. if so, student gets returned in result.
your second requirement different, if understand correctly. have 2 collections of studentes, , want know students in st
.
var intersectedlist = students.intersect(st);
Comments
Post a Comment