views:

111

answers:

5

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?

A: 

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
A: 

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

Dan F
A: 
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();
David B
A: 
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
A: 

Hi,

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

Cheers

Ramesh Vel

Ramesh Vel