tags:

views:

192

answers:

3

Hello there,

i have a class user, and i also have implemented a collection for the same user class - users. users will store user objects. users collection class implements the generic collection class - collection.

i want to cast the LINQ query result into an object of my collection class - users. Everytime i try explicit casting, it gives me a cannot class runtime exception.

i tried to implement the iEnumerable interface also in my users collection class, but still i could not cast the query result.

Now i have to do a for loop within the query result set, and fill up my own custom collection - users.

please help me regarding this, thanx in advnce. Sanjay Sachdev

+2  A: 

Why not create a constructor in your Collection class that takes an IEnumerable as a param and encapsulates the loop for you? Try this in a console app to see what i'm suggesting - in your case, you'd be doing a Collection - i just used int for ease of creating an example...

class Program
{
    static void Main(string[] args)
    {
        MyCollection myCollection = new MyCollection(Enumerable.Range(1, 10));

        foreach (var item in myCollection)
        {
            Console.WriteLine(item);
        }
    }
}

public class MyCollection : Collection<int>
{
    public MyCollection(IEnumerable<int> input)
    {
        foreach (var item in input)
        {
            this.Add(item);
        }
    }
}

You could also create an extension method to make it easier to use...

public static class MyCollectionHelper
{
    public static MyCollection ToMyCollection(this IEnumerable<int> input)
    {
        return new MyCollection(input);
    }
}

and then you'd call it like this...

MyCollection myCollection = Enumerable.Range(1,10).ToMyCollection();
Scott Ivey
+1 - if you're deriving from List<T>(), you get this constructor for free. It's also worth considering if you even need a specialized collection type. If the collection wouldn't have any specialized state, you could consider using extension methods on IEnumerable<User> instead.
dahlbyk
As a note - i'd rather inherit from List<T> over Collection<T> too...
Scott Ivey
A: 

You can't cast an object into something that it isn't, even if they both implement the same interface.

For example you can't cast an int array into a List<int> eventhough they both implement IEnumerable<int>. You can cast either of them to the interface, but that doesn't change the actual type of the object. The array is still an array, and it can't be a list.

So, you have to loop through the result and put each item in your collection.

When it's a result from a LINQ query there is another reason why you have to loop through the result. What the query returns is not a collection with the finished result, but an object that will create the result when you loop through it. So, the result doesn't even exist before you loop through it.

Guffa
+1  A: 

It is not 100% clear what you need from the question, but have you tried the Cast extension method?

kek444