1

I have this method:

private static List<ObjectA> ListItemsToShow(IEnumerable<ObjectA> listObjectB, 
                                             string _rarity, 
                                             string _type, 
                                             string _color,
                                             int? _quantity, 
                                             int? _sold, 
                                             string _quantitySymbol,
                                             string _soldSymbol)
{
    List<ObjectA> listToReturn = (from item in listObjectB 
                                  where _rarity == "All" || item.rarity == _rarity
                                  where _type == "All" || item.Type.Contains(_type) 
                                  where _color == "All" || item.Color.Contains(_color) 
                                  where _quantity == null || item.NbInStock == (int)_quantity
                                  where _sold == null || item.QtySold == (int)_sold
                                  select item).ToList();
    return listToReturn;
}

Up to now it does it job: based on a static List of objects, it returns what can be filtered from the list of original objects.

Now I want to add a dynamic parameter: the quantitySymbol and soldSymbol. Each would be one of those choices:

  • >
  • <
  • >=
  • <=

So that I may get, for example, all items which NbInStock is <, >, <= or >= than those retained in the original list. The same would apply for the QtySold property.

I have a bit of trouble figuring how I could do it in a linq statement, I'd need help to make this out.

1 Answer 1

3

You could use a functional for filtering:

List<ObjectA> ItemsToShow(IEnumerable<ObjectA> listObjectB, Func<int,bool> stockFilter) {
  return (
    from item in listObjectB
    where (stockFilter == null || stockFilter(item.NbInStock)
    select item
  ).ToList();
}

and use it like this:

ItemsToShow(data, stock => (stock <= 10));
ItemsToShow(data, stock => (stock == 25));
ItemsToShow(data, stock => (stock > 3));
ItemsToShow(data, null); // does not use a stock filter

If you need to create the stock filter from a string you can use a factory function:

Func<int,bool> CreateCompareFilter(string op, int? quantity) {
   if(quantity == null) return null;
   if(op == "==") return x => (x == quantity);
   if(op == "<") return x => (x < quantity);
   ...
   return null;
}

Then you can write

ItemsToShow(data, CreateCompareFilter("==",10));

So in total this would look like this:

private static List<ObjectA> ListItemsToShow(IEnumerable<ObjectA> listObjectB, 
                                             string _rarity, 
                                             string _type, 
                                             string _color,
                                             Func<int,bool> _stockFilter,
                                             Func<int,bool> _soldFilter)
{
    return (
        from item in listObjectB 
        where _rarity == "All" || item.rarity == _rarity
        where _type == "All" || item.Type.Contains(_type) 
        where _color == "All" || item.Color.Contains(_color) 
        where _stockFilter == null || _stockFilter(item.NbInStock)
        where _soldFilter == null || _soldFilter(item.QtySold)
        select item
    ).ToList();
}

ListItemsToShow(data, rarity, type, color,
   CreateCompareFilter(_quantitySymbol,_quantity)
   CreateCompareFilter(_soldSymbol,_sold));
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, but how could I evaluate which stock filter I may get from a string? The string is obtained from a view / dropdownlist value in an Ajax call.
Yes, I will try it out, that's very interesting.
I'm trying to understand. That will create a lamdba expression, correct? So how could I mix it with my statement like I made up above?
Ah! Ok! The stockfilters are made before calling the method. Thanks!

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.