在连接表中查找LINQ数组元素

本文关键字:LINQ 数组元素 查找 连接 | 更新日期: 2023-09-27 18:07:28

我有两个表Student和YearAcademy。我只选择Student表,但将YearAcademy表与Student表连接。

假设,在join YearAcademy表行中,卷号为1,2,3,4,5,6,7,8,9,10,11,12…

i want Search: List rollno = {1,3,5};

如何将rollno作为整型值列表传递给YearAcademy ?rollno字段?

谁能帮助我知道如何设置where子句搜索数组列表到yearacademy加入表?

public ActionResult AttendanceStudentList(FormCollection form)
        {
            List<ViewModel.StudentViewModel> viewModel = new List<ViewModel.StudentViewModel>();
            string yearacademy = AcademyYear;
            int studentclassid = Convert.ToInt32(form["classid"]);
            string studentdivision = form["division"];
            string rno = "1,3,2";// form["rollno"];
            List<int> rollno = { 1,2,3,4};// new List<int>(rno.Split(','));
        string grno = form["grno"];
        using (var context = new emsEntities())
        {
            var DataTable = from YearAcademy in context.tbl_YearAcademy
                            join Student in context.tbl_Student
                            on YearAcademy.Id_Student equals Student.Id_Student 
                            where
                                 (YearAcademy.FromToYear == yearacademy)
                            where
                                 (YearAcademy.Id_ClassLevel == studentclassid)  && rollno.Contains(YearAcademy.RollNo.Value.ToString()))

                            select 
                             Student;
            foreach (var iModel in DataTable)
            {
                tbl_Student obj = iModel;
                StudentViewModel vm = new StudentViewModel();
                vm = ParseObject(vm, iModel);
                viewModel.Add(vm);
            }

        }
        ViewBag.Class = studentdivision;
        return PartialView("StudentAttendanceList", viewModel);
    }

在连接表中查找LINQ数组元素

你可以尝试这样做:

var DataTable = from YearAcademy in context.tbl_YearAcademy
                join Student in context.tbl_Student
                on YearAcademy.Id_Student equals Student.Id_Student 
                where YearAcademy.FromToYear == yearacademy &&
                      YearAcademy.Id_ClassLevel == studentclassid  &&  
                      rollno.Contains(YearAcademy.RollNo)
                select Student;

您不必在YearAcademy.RollNo.Value上调用ToString()方法。此外,您必须将rollno的声明更改为以下声明:

List<int?> rollno = { 1,2,3,4 };