Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Wed, 27-Aug-2014 Authors
All Time Authors
Vishal@Neeraj
31680
Rajkatie
20600
Bharathi Cherukuri
18040

Latest members | More ...


(Statistics delayed by 5 minutes)
 Interview Question

Answer:

This can be done by negating the "Contains" extension method . Example follows.

Suppose we have a person class as under

public class Person

{
public string PersonName { get; set; }
public string JobTitle { get; set; }

public override string ToString()
{
return "Person Name: " + PersonName + " JobTitle:" + JobTitle;
}
}


Then populate the Person collection as under

private static List<Person> PreparePersonCollection()

{
List<Person> lstPersonCollection = new List<Person>();
lstPersonCollection.Add(new Person { PersonName = "Amitav Sen", JobTitle = "Design Engineer" });
lstPersonCollection.Add(new Person { PersonName = "Bhavini Dey", JobTitle = "Software Engineer" });
lstPersonCollection.Add(new Person { PersonName = "Debasis Basu", JobTitle = "Software Engineer" });
lstPersonCollection.Add(new Person { PersonName = "Kartik Moin", JobTitle = "Lead Engineer" });
lstPersonCollection.Add(new Person { PersonName = "Shahjahan Khan", JobTitle = "Technical Lead" });

return lstPersonCollection;
}


Suppose we want to get the list of Person who does not has job Title as "Design Engineer", "Software Engineer".

The complete query will be as under

List<Person> source = PreparePersonCollection();


string[] strJobTitleToInclude = new string[] { "Design Engineer", "Software Engineer" };

//Linq Version

(from s in source
where !strJobTitleToInclude.Contains(s.JobTitle)
select s)
.ToList()
.ForEach(i => Console.WriteLine(i.ToString()));

//Lambda Version
source
.Where(i => !strJobTitleToInclude.Contains(i.JobTitle))
.ToList()
.ForEach(i => Console.WriteLine(i.ToString()));


Output

Person Name: Kartik Moin JobTitle:Lead Engineer

Person Name: Shahjahan Khan JobTitle:Technical Lead


Asked In: Many Interviews | Alert Moderator 

Found interesting? Add this to:



Comments or Responses

Login to post response