0

I have a list of objects that I want to filter based on some information. For example list of fruits that may have different fruits. from this list of fruits I want to get a new list of only fruits that match a certain criteria.

I have:

public List<Fruit> getFruitList(List<Fruit> allFruits, string fruitType, string fruitColor) {

 List<Fruit> newFruits = allFruits.Where(f => f.typeOfFruit == fruitType)
                               .Where(f => f.fruitColor == fruitColor).ToList();
 return newFruits;
}

but what happens if someone wants all fruit types, but wants to specify only on color (red, meaning red apples and strawberries should appear in the new list)? this means that the fruitType parameter can come as "All", which would not filter out any fruit based on that criteria.

the fruits in the list though, don't have a typeOfFruit value of "All" so the filter will return no results because no fruit had that value "All"

I saw something like this:

query = fruitColor == "All" ? query : query.Where(x => x.fruitColor == fruitColor);
query = fruitType == "All" ? query : query.Where(x => x.typeOfFruit == fruitType);

and build a query this way, however I don't know what kind of object this "query" is. Is it a string? or is there some sort of "Query" class that I can use to build a query expression?

4 Answers 4

3

A simple modification to take care of ALL the fruit types would be..

List<Fruit> newFruits = allFruits.Where(f => f.typeOfFruit == fruitType || fruitType == "All")
                               .Where(f => f.fruitColor == fruitColor).ToList();

The f.typeOfFruit == fruitType || fruitType == "All" part would take care of a particular criteria of the fruit OR All types of fruits, respectively

Your LINQ can further be simplified to

List<Fruit> newFruits = allFruits
                        .Where(f => (f.typeOfFruit == fruitType || fruitType == "All") 
                                 && f.fruitColor == fruitColor).ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

can you explain what that top line is doing with the or '||'
I think! It should not be f.typeOfFruit...!
Instead of f.typeOfFruit it should be fruitType like this f => (f.typeOfFruit == fruitType || fruitType == "All")
There isn't a fruit of type "All". The "All" is possible parameter in the calling method.
1

If you are using System.Linq then it should be IQueryable. You can see the documentation here.

Edit: Adding Enumerable.Where documentation here.

Comments

0

This demonstrates how you can dynamically build linq queries:

public List<Fruit> getFruitList(List<Fruit> allFruits, string fruitType, string fruitColor) 
{

    IEnumerable<Fruit> newFruits = allFruits;

    if(fruitType != "All")
    {
        newFruites = newFruits.Where(f => f.typeOfFruit == fruitType);
    }
    if(fruitColor != "All")
    {
        newFruits = newFruits.Where(f => f.fruitColor == fruitColor);
    }
    return newFruits.ToList();
}

1 Comment

I see, but I'm trying to avoid multi if/else blocks, I should've clarified in the question, my apologies
0

This should do the trick, just pass a null or empty string for the parameter you do not want to filter by

public List<Fruit> getFruitList(List<Fruit> allFruits, string fruitType, string fruitColor) {

	bool filterByFruitType = !string.IsNullOrEmpty(fruitType);
	bool filterByFruitColor = !string.IsNullOrEmpty(fruitColor);
	
	// both type and color
	if (filterByFruitType && filterByFruitColor) {
		return allFruits.Where(f => f.typeOfFruit == fruitType)
                               .Where(f => f.fruitColor == fruitColor).ToList();
	}
	// color only
	else if (!filterByFruitType && filterByFruitColor) {
		return allFruits.Where(f => f.fruitColor == fruitColor).ToList();
	}
	// type only
	else if (filterByFruitType && !filterByFruitColor) {
		return allFruits.Where(f => f.typeOfFruit == fruitType).ToList();
	}
	// no filter
	else {
		return allFruits;
	}
}

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.