8

There is a class

Public class Student
{
  int id;
  int rollNum;
  String Name;
}

a method

Public List<Student> GetAllStudents()
{
  var studentList = GetMeAll(); // Simple select * query to get all students
  //unfortunately i can't make changes in this method so have to write a linq query to       //filter data.
  //getting int array ids correctly from some method
   var filterList = FilterStudentsByID(ids,StudentList);
}

I want to write a method that takes an int array as input and returns a List

Public List<Student> FilterStudentsByID(int[] ids,List<student> studentList)
{
  //There should be a linq query , but I am new to linq so don't know how to write this.
  Var list = from  studentList in ..... don't know much;
}

Any help in writing the query. Thank you all for Answers, I am also looking for maintain the performances with large list of records ?? It will be great if you can add me simple explanation of the query will work internally ??

3
  • Does each student have a unique ID? Commented Mar 13, 2013 at 9:06
  • Also: Will the list of ids be long (more than 5% of the size of the student list, say)? Just trying to work out if it's worth creating a list of students sorted by ID before doing the searching. Commented Mar 13, 2013 at 9:07
  • yes it could be going around 5% of the size Commented Mar 13, 2013 at 9:09

3 Answers 3

12

If you are looking to find those students from the list whose id's are present in the ids array then:

var list = from s in stundentList
           where ids.Contains(s.ID)
           select s;
Sign up to request clarification or add additional context in comments.

6 Comments

Can you explain me how this will work internally ,like will it hamper performance in case of large list??
Looks like it would be O(N*M) where N = student count, M = ID count. But you can't do much better, I think.
@ankur, LINQ internally uses iteration, so it will check against each student id against the id array. This is useful when interacting with the database, since that will create a Select * from table where ID in (1,2,3) kind of query. More details on wekeroad.com/2008/02/27/creating-in-queries-with-linq-to-sql
Unfortunately it won't be executed on the database in this case, as "studentList" is a List and not an IQueryable.
@JustAnotherUserYouMayKnow, yes that is in the question
|
5

try

Var list = studentList.Where(s=>ids.Contains(s.id)).ToList();

Comments

1

I think the answers already given will be fast enough, but just in case after testing you find that it's too slow: You can try converting the student list to a dictionary before doing the searching.

You should only use this after careful timing tests, since it is likely to be a lot slower for most cases!

public static List<Student> FilteredStudents(int[] targetIds)
{
    var allStudents = GetAllStudents().ToDictionary(student => student.id);
    var result = new List<Student>();

    foreach (int id in targetIds)
    {
        Student student;

        if (allStudents.TryGetValue(id, out student))
        {
            result.Add(student);
        }
    }

    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.