Linq and casting

  • Thread starter Thread starter Techno_Dex
  • Start date Start date
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;}
}
 
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 isnot
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?

Yes. It's "let":

from Food oFood in Foods
let fruit = oFood.ItemCategory as Fruit
where fruit != null
let orange = fruit.Item as Orange
let apple = fruit.Item as Apple
where (orange != null && orange.Color == "Orange") || (apple != null
&& apple.HasCore)
select oFood;
 
Much appriciated as this will come in handy to shorten hierarchy chains as
well which don't really need the hierarchy

let Foo = x..y.z
where Foo.ContainsSomething == true &&
Foo.IsValue == true




Yes. It's "let":

from Food oFood in Foods
let fruit = oFood.ItemCategory as Fruit
where fruit != null
let orange = fruit.Item as Orange
let apple = fruit.Item as Apple
where (orange != null && orange.Color == "Orange") || (apple != null
&& apple.HasCore)
select oFood;
 
Peter Duniho said:
Duh. How could I forget about that?

Scratch my previous post, MSDN'ed it right after I posted and found that you
can do it in VB.Net too! :)
 
Back
Top