简体   繁体   English

在多个表之间搜索功能

[英]Search functionality between more than one table

I'm new to ASP.NET MVC C# with EF6 in SQL Server, I have 3 tables:我是 SQL Server 中带有 EF6 的 ASP.NET MVC C# 的新手,我有 3 个表:

  1. Student Info学生信息
  2. Courses培训班
  3. Stud_Courses to mapping between students and courses Stud_Courses 在学生和课程之间进行映射

Sample data:样本数据:

Cou_ID | Stud_ID
-------+--------
     1 | 1
     1 | 2

I want to do searching in student page by student name or by course name for example if a user type math it will represents all students takes math I wrote this code that search by name correctly the problem is search by course name show error at s.Stud_ID in this line我想按学生姓名或课程名称在学生页面中进行搜索,例如,如果用户键入数学,它将代表所有学生进行数学我编写了此代码,按名称正确搜索问题是按课程名称搜索显示错误在 s。此行中的 Stud_ID

StuList = db.Students.Where(x => x.StudentId == s.Stud_ID).ToList();

(Note : Stud_ID type is varchar in the database) (注:数据库中Stud_ID类型为varchar)

Stud_Courses does not contain a definition for 'Stud_ID' and no extension method 'Stud_ID' accepting a first argument of type 'List' could be found (are you missing a using directive or an assembly reference Stud_Courses不包含“Stud_ID”的定义,并且找不到接受“List”类型的第一个参数的扩展方法“Stud_ID”(您是否缺少 using 指令或程序集引用

My controller code我的控制器代码

public JsonResult GetSearchingData(string SearchBy, string SearchValue){

List<Students> StuList = new List<Students>();

if (SearchBy == "Name")    
{
    StuList = db.Students.Where(x => x.StudentsName.Contains(SearchValue) || SearchValue == null).ToList();
    var subCategoryToReturn = SupList.Select(S => new { supervisorName = S.supervisorName });
    return Json(SupList, JsonRequestBehavior.AllowGet);   
}

if (SearchBy == "Course Name")
{
    var c = db.Courses.Where(x => x.CoursesName == SearchValue).SingleOrDefault();
    var CN = c.CourseId;
    var s = db.Stud_Courses.Where(x => x.Cou_ID == CN).ToList();
    StuList = db.Students.Where(x => x.StudentId == s.Stud_ID).ToList();  
    // here the error happens
    return Json(SupList, JsonRequestBehavior.AllowGet);}
}

Stud_Courses model Stud_Courses 模型

namespace MyModel.Models{
using System;
using System.Collections.Generic;

public partial class Stud_Courses{
    public int Id { get; set; }
    public Nullable<int> Cou_ID { get; set; }
    public string Stud_Id { get; set; }

    public virtual Courses Courses{ get; set; }
    public virtual Student Student { get; set; }
}}

"s" is a List so you need to select the Stud_Id, so s will be a List. "s" 是一个列表,因此您需要选择 Stud_Id,因此 s 将是一个列表。 Then you can get all students that are contained in s.然后你可以得到s中包含的所有学生。

var s = db.Stud_Courses.Where(x => x.Cou_ID == CN).Select(x => x.Stud_Id).ToList();
StuList = db.Students.Where(x => s.Contains(x.StudentId)).ToList();

or you can reduce the 3 separate queries to或者您可以将 3 个单独的查询减少到

StuList = db.Students.Where(x => x.Stud_Courses.Any(y => y.Courses.CoursesName == SearchValue)).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM