Ozmen
Ozmen

Reputation: 139

How to check Multiple values at list in one condition with linq?

I can check with 2 conditions like below, but in aspect of performance , is there any better way to do this? I want it to return true when list includes records whose StudentName is John and Ram. If there is just John, it needs to return false

studentList.Any(o => o.StudentName.Equals("John")) 
                     && studentList.Any(o => o.StudentName.Equals("Ram"));

will cause getting Students list 2 times, may I check if multiple values exist in list in one condition?

using System;
using System.Linq;
using System.Collections.Generic;

                    
public class Program
{
    public static void Main()
    {
        // Student collection
        IList<Student> studentList = new List<Student>() { 
                new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
                new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 } 
            };
        
        // LINQ Query Syntax to find out teenager students
        
        var bool_check = studentList.Any(o => o.StudentName.Equals("John")) 
            && studentList.Any(o => o.StudentName.Equals("Ram"));

        Console.WriteLine(bool_check);
    }
}

public class Student 
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }
    public int Age { get; set; }
}

Upvotes: 2

Views: 5053

Answers (3)

In performance there is nothing that makes a big difference but in syntax you can use multiple conditions like this:

IList<Student> studentList = new List<Student>() {
                new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
                new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
                new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
                new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
                new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
            };


var test = from s in studentList
           where (s.StudentName!=null && s.StudentName!="") && (s.StudentName.Contains("John") || s.StudentName.Contains("Ram"))
           select s;

foreach (var item in test)
{
    Console.WriteLine($"id: {item.StudentID} name : {item.StudentName}");
}

Console.ReadKey();

This way in the "where" you can apply all the necessary filters as if it were a single condition

Upvotes: 0

DooDoo
DooDoo

Reputation: 13447

List<string> DesireNames = new List<string>(){"John", "Ram"};
var YourFilteredStudents = ShortList.Where(o=> DesireNames.Contains(o.StudentName ));

Upvotes: 1

Markus
Markus

Reputation: 22456

The current condition would search the list twice if "John" was found. If you want to avoid this, you could filter for both "John" and "Ram" and check this smaller list, e.g.:

var shortList = studentList
                  .Where(x => x.StudentName == "John" 
                              || x.StudenName == "Ram")
                  .ToHashSet();
var bool_check = shortList.Contains("John") && shortList.Contains("Ram);

However, as long as you have the list in memory and the size is not too big, the difference will be subtle.

Upvotes: 2

Related Questions