Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Check if a List is a part of another List [duplicate]

I have two lists as follows

 var query1 = from enrollments in db.Enrollments
             where enrollments.studentID == studentID && enrollments.result >= 50 && enrollments.enrolled == false
             select enrollments.subjectID;
 var query2 = from prerequisites in db.Prerequisites
              where prerequisites.subjectID == subjectID
              select prerequisites.prerequisiteID;

Now I want to make sure that all the numbers in query2 exist in query1. In other words, I want to ensure that query2 is a part of query1

Any ideas?

P.S

-You can be sure that subjectID and prerequisiteID are the same thing

-I can convert query1 and query2 to lists like that query.ToList()

like image 282
RonaDona Avatar asked May 28 '13 10:05

RonaDona


1 Answers

bool results =  query2.All(i=>query1.Contains(i));

related questions below :

Determine if a sequence contains all elements of another sequence using Linq

Check whether an array is a subset of another

like image 158
Damith Avatar answered Oct 24 '22 16:10

Damith