Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Simple Linq to Object example with Casting

This is my first go at playing with Linq.  So far it is pretty cool, but I wanted to share something because I found it painful. 

I wanted query a list and then cast a anonymous type to a static type.  I was not able to find anything on the net to help me with this (could be i just did not look in the right places) so i thought i would share my findings. 

Below is the definition of the 'Sport' object

[Serializable]
public class Sport
{

        public virtual Int32 SportID { get; set; }
        public virtual string Name { get; set; }
        public virtual string Description { get; set; }

}

Here is the code to query some data out of a list of sports List<Sport>

List sports = new List();
sports.Add(new Sport { SportID = 1, Name = "Sport 1", Description = "Sport Desc 1" });
sports.Add(new Sport { SportID = 2, Name = "Sport 2", Description = "Sport Desc 2" });
sports.Add(new Sport { SportID = 3, Name = "Sport 3", Description = "Sport Desc 3" });
sports.Add(new Sport { SportID = 4, Name = "Sport 4", Description = "Sport Desc 4" });
sports.Add(new Sport { SportID = 5, Name = "Sport 5", Description = "Sport Desc 5" });
            
var query = from s in sports
	where s.Name == "Sport 2"
        select s;

The code below will show how to cast/convert from anonymous type to static type.

Sport sport = (Sport) query.First();

The code below will show how to cast back to a List<Sport>

List newSports = query.ToList();

Hope this helps someone else.

Till next time,



Comments

Garry Shutler said:

If I need the query outside the scope I've generally made the query go straight into the type I need:

IEnumerable<Sport> sports = from....etc

If you need to make it of a different type I make each result of a given type:

IEnumerable<SpecificClass> list = from s ... etc

select new SpecificClass(s.Name, s.Age);

# February 22, 2008 10:45 AM

Christopher Bennage said:

Good stuff.

# February 22, 2008 10:56 AM

Daily Bits - February 23, 2008 | Alvin Ashcraft's Daily Geek Bits said:

Pingback from  Daily Bits - February 23, 2008 | Alvin Ashcraft's Daily Geek Bits

# February 23, 2008 12:06 PM

Igor Ostrovsky said:

The code is actually *not* casting from an anonymous type to a user-defined type.

The objects you add to the sports list are already of type Sport, not of an anonymous type. The following constructs an object of type Sport, and also immediately initializes its properties:

new Sport { SportID = 5, Name = "Sport 5", Description = "Sport Desc 5" }

To construct an object of an anonymous type, you need to do the following:

new { SportID = 5, Name = "Sport 5", Description = "Sport Desc 5" }

If you change your code sample so that it really constructs objects of an anonymous type, you'll see that the cast no longer works.

# February 24, 2008 9:26 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News