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

时间:2014-06-07 07:11:31

标签: c# linq entity-framework

我有两张桌子Student和YearAcademy。我只选择学生表,但加入了YearAcademy表和学生表。

假设,在连接YearAcademy表行中有卷号1,2,3,4,5,6,7,8,9,10,11,12 ...,99

我想搜索:列表rollno = {1,3,5};

我如何将rollno作为int值列表传递给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);
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

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;

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

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