T
TechnoDex
I'm trying to process through hierarchy of objects which use interfaces
in order to extract out some data but having difficulty with some of the
logic using Linq to Objects. So far the only solution I have seen is
using a ternary operator which is nasty and convoluted in any language.
My sample below is nasty as well (my appologies in advance). Basically
because I have either inherited objects, interfaces or collections of
things of type object, I need to cast data while processing through the
hierarchy and use the appropriate properties along the way (which may
not be the same at each level). I was hoping to avoid writing Methods
for each given type to process as well, but rather push the limits of
Linq to see what it can do. If you look below, after the two "let"
assignments is where I would like to use a switch/case/ifelse logic to
process the hierarchies in their different ways.... Any ideas???
from RefridgeItem in Refridge.ColdFood
let fFruit = RefridgeItem.Item as Fruit
let vVeggy = RefridgItem.Item as Veggy
where (fFruit != null)
from Apple aApple in fFruit.Items.OfType<Apple>()
where aApple.Color == Color.Red
select RefridgeItem
where (vVeggy != null)
from (Pepper pPepper in vVeggy.Items.OfType<Pepper>()
where pPepper.Color == Color.Red
select RefridgeItem
public IFood
{
Color FoodColor { get; set:}
decimal Weight { get; set; }
}
public Food : IFood
{
Color FoodColor { get; set:}
decimal Weight { get; set; }
}
public Veggy
{
IEnum<object> Items { get; set; }
}
public Pepper : Fruit
{
}
public Tomato : Fruit
{
}
public Fruit
{
IEnum<object> Items { get; set; }
}
public Orange : Food
{
}
public Apple : Food
{
}
public RefrigeItem
{
object Item
}
public Refridge
{
//A collection of RefridgeItem objects
RefridgeItems ColdFood { get; set; }
}
in order to extract out some data but having difficulty with some of the
logic using Linq to Objects. So far the only solution I have seen is
using a ternary operator which is nasty and convoluted in any language.
My sample below is nasty as well (my appologies in advance). Basically
because I have either inherited objects, interfaces or collections of
things of type object, I need to cast data while processing through the
hierarchy and use the appropriate properties along the way (which may
not be the same at each level). I was hoping to avoid writing Methods
for each given type to process as well, but rather push the limits of
Linq to see what it can do. If you look below, after the two "let"
assignments is where I would like to use a switch/case/ifelse logic to
process the hierarchies in their different ways.... Any ideas???
from RefridgeItem in Refridge.ColdFood
let fFruit = RefridgeItem.Item as Fruit
let vVeggy = RefridgItem.Item as Veggy
where (fFruit != null)
from Apple aApple in fFruit.Items.OfType<Apple>()
where aApple.Color == Color.Red
select RefridgeItem
where (vVeggy != null)
from (Pepper pPepper in vVeggy.Items.OfType<Pepper>()
where pPepper.Color == Color.Red
select RefridgeItem
public IFood
{
Color FoodColor { get; set:}
decimal Weight { get; set; }
}
public Food : IFood
{
Color FoodColor { get; set:}
decimal Weight { get; set; }
}
public Veggy
{
IEnum<object> Items { get; set; }
}
public Pepper : Fruit
{
}
public Tomato : Fruit
{
}
public Fruit
{
IEnum<object> Items { get; set; }
}
public Orange : Food
{
}
public Apple : Food
{
}
public RefrigeItem
{
object Item
}
public Refridge
{
//A collection of RefridgeItem objects
RefridgeItems ColdFood { get; set; }
}