如何匹配linq条件中的列表值?

时间:2016-04-06 06:06:17

标签: c# asp.net-mvc entity-framework linq

如何匹配linq条件中的列表值?

我有一个ID列表,所以这些id必须匹配条件并从一个表sol获取数据如何?

public ActionResult StudentSelect(
    long? CompanyId,
    long? CompanyLocationId,
    long? ClassId,
    long? SectionId,
    long? AcademicId,
    long? ExamId) //) //long? SectionId)

RegId以下是学生注册ID

的列表
{
    var RegId = (
        from a in db.Student_Marks_Percentage
        where a.Delete_Flag == false &&
            a.Exam_Id == ExamId &&
            a.Academic_Year_Id == AcademicId &&
            a.Class_Id == ClassId
        select a.Registration_Id)
        .ToList();

    List<StudentList> Student = new List<StudentList>();

    if (AcademicId != null &&
        CompanyId != null &&
        CompanyLocationId != null &&
        ClassId == null &&
        SectionId == null) //&& )//&& ClassId != null) //)
    {
        Student = (
            from a in db.Student_Re_Admission
            join b in db.Student_Registration on a.Registration_Id equals b.Registration_Id
            join c in db.Student_Roll_No_Assign on a.Registration_Id equals c.Registration_Id
            where c.Academic_Year_Id == AcademicId &&
                c.Company_ID == CompanyId &&
                c.COMPANY_LOCATION_ID == CompanyLocationId &&
                a.Academic_Year_Id == AcademicId &&
                c.Class_Id == ClassId &&
                a.Class_Id == ClassId &&
                a.Section_Id == SectionId &&
                c.Section_Id == SectionId &&

在这里,我必须将所有id列表与a.Registration_Id

进行比较
                a.Registration_Id != RegIds &&
                a.Promoted == false &&
                a.Delete_Flag == false
                //a.Academic_Year_Id == AcademicId &&
                //a.Company_ID == CompanyId &&
                //a.COMPANY_LOCATION_ID == CompanyLocationId
            select new StudentList()
            {
                Registration_Id = a.Registration_Id,
                Admission_No = a.Admission_No,
                Student_First_Name = a.Student_First_Name,
                Student_Middle_Name = a.Student_Middle_Name,
                Student_Last_Name = a.Student_Last_Name,
                Set_Roll_No = c.Set_Roll_No,
                Roll_Id = c.Roll_Id
            })
            .OrderBy(a => a.Registration_Id)
            .ToList();

            ViewBag.StudentList = Student.ToList();
            return PartialView();
        }
   }

我尝试过以下但得到同样的学生名单数据..

var merged = new List<StudentList>(Student);
                merged.Except(Student.Where(p2 => RegId.Exists(p1 => p2.Registration_Id != p1.Registration_Id)));

2 个答案:

答案 0 :(得分:0)

我认为您只需要将a.Registration_Id != RegIds替换为:

!RegIDs.Contains(a.Registration_Id)

答案 1 :(得分:0)

必须比较的第一个var列表

var RegId = (from a in db.Student_Marks_Percentage
                         where a.Delete_Flag == false
                         && a.Exam_Id == ExamId
                        && a.Academic_Year_Id == AcademicId
                        && a.Class_Id == ClassId
                         select a).ToList();

必须获取哪些数据的第二个列表

List<StudentList> Student = new List<StudentList>();

            if (AcademicId != null && CompanyId != null && CompanyLocationId != null && ClassId == null && SectionId == null) //&& )//&& ClassId != null) //)
            {
                Student = (from a in db.Student_Re_Admission
                           join b in db.Student_Registration on a.Registration_Id equals b.Registration_Id
                           join c in db.Student_Roll_No_Assign on a.Registration_Id equals c.Registration_Id                           

                           where c.Academic_Year_Id == AcademicId && c.Company_ID == CompanyId && c.COMPANY_LOCATION_ID == CompanyLocationId
                           && a.Academic_Year_Id == AcademicId && c.Class_Id == ClassId && a.Class_Id == ClassId
                           && a.Section_Id == SectionId && c.Section_Id == SectionId
                           //&& a.Registration_Id != RegId.Contains(a.Registration_Id)
                           && a.Promoted == false && a.Delete_Flag == false
                           //a.Academic_Year_Id == AcademicId && a.Company_ID == CompanyId && a.COMPANY_LOCATION_ID == CompanyLocationId

                           select new StudentList()
                           {
                               Registration_Id = a.Registration_Id,
                               Admission_No = a.Admission_No,
                               Student_First_Name = a.Student_First_Name,
                               Student_Middle_Name = a.Student_Middle_Name,
                               Student_Last_Name = a.Student_Last_Name,
                               Set_Roll_No = c.Set_Roll_No,
                               Roll_Id = c.Roll_Id

                           }).OrderBy(a => a.Registration_Id).ToList();
}

通过搜索以下过程合并两个列表。

var merged = Student.Where(a1 => !RegId.Any(a2 => a1.Registration_Id == a2.Registration_Id));