simple select query in linq

Lets say I have a student table and I want to display the student with ID 1.

SELECT *
FROM STUDENT ST
WHERE ST.ID = 1

This is how I achive this in Linq.

StudentQuery = from r in oStudentDataTable.AsEnumerable()
                                     where (r.Field<int>("ID") == 1)
                                     select r;
            oStudentDataTable = StudentQuery.CopyToDataTable();

but what if I want to display the students with these ids 1,2,3,4,5..

SELECT *
FROM STUDENT ST
WHERE ST.ID IN (1,2,3,4,5)

How can I achieve this in Linq?

Answers


Use .Contains

var list = new List<int> { 1, 2, 3, 4, 5 };

var result = (from r in oStudentDataTable.AsEnumerable()
              where (list.Contains(r.Field<int>("ID"))
              select r).ToList();

Try IEnumerable.Contains:

var list = new List<int>(){1,2,3,4,5};
StudentQuery = from r in oStudentDataTable.AsEnumerable()
                                 where (list.Contains(r.Field<int>("ID")))
                                 select r;
        oStudentDataTable = StudentQuery.CopyToDataTable();

Try this also :

var list = new List<int> { 1, 2, 3, 4, 5 };

List<StudentQuery> result = (from r in oStudentDataTable.AsEnumerable()
              where (list.Contains(r.Field<int>("ID"))
              select new StudentQuery
              { /*
                .Your entity here
                .
                */
              }).ToList<StudentQuery>();

Need Your Help

What does the term Plain Old Java Object (POJO) exactly mean?

java terminology pojo

What does the term Plain Old Java Object (POJO) mean? I couldn't find anything explanatory enough.

Adding code to __init__.py

python initialization package

I'm taking a look at how the model system in django works and I noticed something that I don't understand.