Jump to content
Sign in to follow this  
Girish Dubey

Filter List the data with Dictionary condition using linq

Recommended Posts

How i can filter List the data with Dictionary condition using linq.

class Person
{
	public string name
	{
		get; set;
	}

	public string subject
	{
		get; set;
	}

	public string type
	{
		get; set;
	}

	public int age
	{
		get; set;
	}
}

class Test
{
	public void FindData()
	{
		// Create dictionary and add five keys and values.
		var conditionDictionary = new Dictionary<string, string>();
		conditionDictionary.Add("subject", "english");
		conditionDictionary.Add("type", "teacher");
		
		List<Person> persons = new List<Person>();
		persons.Add(new Person
		{
			name = "Rajul",
			subject = "math",
			type = "teacher",
			age = 20
		});
		persons.Add(new Person
		{
			name = "Jhon",
			subject = "english",
			type = "teacher",
			age = 25
		});
		persons.Add(new Person
		{
			name = "Nitin",
			subject = "english",
			type = "student",
			age = 21
		});
		persons.Add(new Person
		{
			name = "Sumit",
			subject = "english",
			type = "teacher",
			age = 26
		});
		persons.Add(new Person
		{
			name = "Komal",
			subject = "science",
			type = "student",
			age = 29
		});
		
		List<Person> filteredpersons = Filter(persons, conditionDictionary);
	}

	public List<Person> Filter(List<Person> persons, Dictionary<string, string> conditionDictionary)
	{
      
      /*TODO: can you suggest how to filter list data using linq with Distionary*/
      
		var list = (from person in persons
                        from condition in conditionDictionary
                        select person).ToList();
		return list;
	}
}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...