T
Techno_Dex
Does Linq have any way of assignment to a given type? My problem is I have
nested objects where some of the properties in the Nest are of type Object
which can be one of many types but the filtering of Linq starts to get nasty
when checking "is type" then casting to that type in order to use the object
and it gets worse the deeper you go. Unfortunately the object model is not
mine so I can't change it. Using the classes below, say I want the Food
Items which are Fruits which have a Color or Orange or has a Core. Having
to test the type then cast to the type continually to access properties gets
very nasty very quickly and when going deeper in the object model is nasty.
Is there any way to declare a variable of that type or subquery which would
avoid having to check the type then cast to that type for every use of the
object?
var Results = (from Food oFood in Foods
where (oFood.ItemCategory is Fruit) &&
(
((Fruit)oFood.ItemCategory).Item is
Orange &&
((Orange)((Fruit)oFood.ItemCategory).Item).Color
== "Orange"
} || {
((Fruit)oFood.ItemCategory).Item is
Apple &&
((Apple)((Fruit)oFood.ItemCategory).Item).HasCore
== true
}
select oFood).ToList();
public class Foods : List<Food>
{
}
public class Food
{
public int ID {get;set;}
[XmlElementAttribute("Veggy", typeof(Veggy))]
[XmlElementAttribute("Fruit", typeof(Fruit))]
public object ItemCategory (get;set;}
}
public class Veggy
{
public DateTime Purchased {get;set;}
[XmlElementAttribute("Carrot", typeof(Carrot))]
[XmlElementAttribute("GreenBean", typeof(GreenBean))]
public object Item {get;set;}
}
public class Fruit
{
public DateTime Purchased {get;set;}
[XmlElementAttribute("Apple", typeof(Apple))]
[XmlElementAttribute("Orange", typeof(Orange))]
[XmlElementAttribute("Bannana", typeof(Bannana))]
public object Item {get;set;}
}
public class Apple
{
public string Color {get;set;}
public bool HasCore {get;set;}
public bool HasSeeds {get;set;}
}
public class Orange
{
public string Color {get;set;}
public bool HasSeeds {get;set;}
}
public class Bannana
{
public bool Color {get;set;}
public bool TastesGood {get;set;}
}
nested objects where some of the properties in the Nest are of type Object
which can be one of many types but the filtering of Linq starts to get nasty
when checking "is type" then casting to that type in order to use the object
and it gets worse the deeper you go. Unfortunately the object model is not
mine so I can't change it. Using the classes below, say I want the Food
Items which are Fruits which have a Color or Orange or has a Core. Having
to test the type then cast to the type continually to access properties gets
very nasty very quickly and when going deeper in the object model is nasty.
Is there any way to declare a variable of that type or subquery which would
avoid having to check the type then cast to that type for every use of the
object?
var Results = (from Food oFood in Foods
where (oFood.ItemCategory is Fruit) &&
(
((Fruit)oFood.ItemCategory).Item is
Orange &&
((Orange)((Fruit)oFood.ItemCategory).Item).Color
== "Orange"
} || {
((Fruit)oFood.ItemCategory).Item is
Apple &&
((Apple)((Fruit)oFood.ItemCategory).Item).HasCore
== true
}
select oFood).ToList();
public class Foods : List<Food>
{
}
public class Food
{
public int ID {get;set;}
[XmlElementAttribute("Veggy", typeof(Veggy))]
[XmlElementAttribute("Fruit", typeof(Fruit))]
public object ItemCategory (get;set;}
}
public class Veggy
{
public DateTime Purchased {get;set;}
[XmlElementAttribute("Carrot", typeof(Carrot))]
[XmlElementAttribute("GreenBean", typeof(GreenBean))]
public object Item {get;set;}
}
public class Fruit
{
public DateTime Purchased {get;set;}
[XmlElementAttribute("Apple", typeof(Apple))]
[XmlElementAttribute("Orange", typeof(Orange))]
[XmlElementAttribute("Bannana", typeof(Bannana))]
public object Item {get;set;}
}
public class Apple
{
public string Color {get;set;}
public bool HasCore {get;set;}
public bool HasSeeds {get;set;}
}
public class Orange
{
public string Color {get;set;}
public bool HasSeeds {get;set;}
}
public class Bannana
{
public bool Color {get;set;}
public bool TastesGood {get;set;}
}