0

I am facing a scenario where I have to filter a single object based on many objects.

For sake of example, I have a Grocery object which comprises of both Fruit and Vegetable properties. Then I have the individual Fruit and Vegetable objects.

My objective is this:

var groceryList = from grocery in Grocery.ToList()
                  from fruit in Fruit.ToList()
                  from veggie in Vegetable.ToList()
                  where (grocery.fruitId = fruit.fruitId)
                  where (grocery.vegId = veggie.vegId)
                  select (grocery);

The problem I am facing is when Fruit and Vegetable objects are empty. By empty, I mean their list count is 0 and I want to apply the filter only if the filter list is populated.

I am also NOT able to use something like since objects are null:

var groceryList = from grocery in Grocery.ToList()
                  from fruit in Fruit.ToList()
                  from veggie in Vegetable.ToList()
                  where (grocery.fruitId = fruit.fruitId || fruit.fruitId == String.Empty)
                  where (grocery.vegId = veggie.vegId || veggie.vegId == String.Empty)
                  select (grocery);

So, I intend to check for Fruit and Vegetable list count...and filter them as separate expressions on successively filtered Grocery objects.

But is there a way to still get the list in case of null objects in a single query expression?

RameshVel
  • 64,778
  • 30
  • 169
  • 213
Jarvis Bot
  • 481
  • 6
  • 15
  • 2
    Are you just trying to do a join with Linq? Sorry I don't fully understand the question. – Simon Fox Sep 17 '09 at 02:53
  • Exactly, just a join. But what is the best approach to take when one of the filter objects is null? – Jarvis Bot Sep 17 '09 at 02:57
  • This is why it's a good idea to forbid null collections. All the code I write enforces that nulls are never used, in favor of empty collections. – bobbymcr Sep 17 '09 at 02:59
  • @bobbymcr - So no redemption? :) – Jarvis Bot Sep 17 '09 at 03:02
  • Actually, I think I misread your question -- you are trying to avoid dereferencing null items inside the list? – bobbymcr Sep 17 '09 at 03:17
  • @bobbymcr thats how I read it at first and why I produced the answer I did, but now I am unsure if that is what the question meant :) – Simon Fox Sep 17 '09 at 03:19
  • Maybe you should post a basic definition of your model, and is it really linq to objects or did you mean linq to sql.... – Simon Fox Sep 17 '09 at 03:34

5 Answers5

0

Try something like the following:

var joined = grocery.Join(fruit, g => g.fruitId,
                                 f => f.fruitId,
                                 (g, f) => new Grocery() { /*set grocery properties*/ }).
                Join(veggie, g => g.vegId,
                             v => v.vegId,
                             (g, v) => new Grocery() { /*set grocery properties*/ });

Where I have said set grocery properties you can set the properties of the grocery object from the g, f, v variables of the selector. Of interest will obviouly be setting g.fruitId = f.fruitId and g.vegeId = v.vegeId.

Simon Fox
  • 10,409
  • 7
  • 60
  • 81
0

I think the LINQ GroupJoin operator will help you here. It's similar to the TSQL LEFT OUTER JOIN

Dan F
  • 11,958
  • 3
  • 48
  • 72
0
IEnumerable<Grocery> query = Grocery

if (Fruit != null)
{
  query = query.Where(grocery =>
    Fruit.Any(fruit => fruit.FruitId == grocery.FruitId));
}

if (Vegetable != null)
{
  query = query.Where(grocery =>
    Vegetable.Any(veggie => veggie.VegetableId == grocery.VegetableId));
}

List<Grocery> results = query.ToList();
Amy B
  • 108,202
  • 21
  • 135
  • 185
0
var groceryList =
  from grocery in Grocery.ToList()
  join fruit in Fruit.ToList()
       on grocery.fruidId equals fruit.fruitId
       into groceryFruits
  join veggie in Vegetable.ToList()
       on grocery.vegId equals veggie.vegId
       into groceryVeggies
  where ... // filter as needed
  select new
  {
    Grocery = grocery,
    GroceryFruits = groceryFruits,
    GroceryVeggies = groceryVeggies
  };
Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
0

You have to use leftouter join (like TSQL) for this. below the query for the trick

private void test()
{
    var grocery = new List<groceryy>() { new groceryy { fruitId = 1, vegid = 1, name = "s" }, new groceryy { fruitId = 2, vegid = 2, name = "a" }, new groceryy { fruitId = 3, vegid = 3, name = "h" } };
    var fruit = new List<fruitt>() { new fruitt { fruitId = 1, fname = "s" }, new fruitt { fruitId = 2, fname = "a" } };
    var veggie = new List<veggiee>() { new veggiee { vegid = 1, vname = "s" }, new veggiee { vegid = 2, vname = "a" } };
    //var fruit= new List<fruitt>();
    //var veggie = new List<veggiee>();

    var result = from g in grocery
                 join f in fruit on g.fruitId equals f.fruitId into tempFruit
                 join v in veggie on g.vegid equals v.vegid into tempVegg
                 from joinedFruit in tempFruit.DefaultIfEmpty()
                 from joinedVegg in tempVegg.DefaultIfEmpty()
                 select new { g.fruitId, g.vegid, fname = ((joinedFruit == null) ? string.Empty : joinedFruit.fname), vname = ((joinedVegg == null) ? string.Empty : joinedVegg.vname) };

    foreach (var outt in result)
        Console.WriteLine(outt.fruitId + "  " + outt.vegid  + "  " + outt.fname  + "  " + outt.vname);
}
public class groceryy
{
    public int fruitId;
    public int vegid;
    public string name;
}
public class fruitt
{
    public int fruitId;
    public string fname;
}
public class veggiee
{
    public int vegid;
    public string vname;
}

EDIT: this is the sample result

1 1 s s

2 2 a a

3 3

vrajs5
  • 4,066
  • 1
  • 27
  • 44
RameshVel
  • 64,778
  • 30
  • 169
  • 213