locked
LINQ to Objects query result cast RRS feed

  • Question

  • 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<user>.

    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 

    Wednesday, July 15, 2009 3:26 PM

Answers

  • Can you show us the relevant code? Or why can't you simply use one of the existing collection classes like List<user>, that way LINQ certainly can easily create one with the ToList() method.

    MVP XML My blog
    • Proposed as answer by Harry Zhu Wednesday, July 22, 2009 6:39 AM
    • Marked as answer by Harry Zhu Thursday, July 23, 2009 9:33 AM
    Wednesday, July 15, 2009 4:53 PM
  • You can't cast one collection type into another (in this case, presumably IQueryable<User> or EntitySet<User> into MyCollection) - you have to repopulate it:

      var result = new MyCollection (db.Users.Where (u => u.Name.StartsWith ("A"));

    Then write a constructor on your collection class to accept IEnumerable<User>

      class MyCollection
      {
         public MyCollection (IEnumerable<User> users)
         {
            foreach (User u in users) Add (u);
         }
      }

    Joe

    Write LINQ queries interactively - www.linqpad.net
    • Proposed as answer by Harry Zhu Wednesday, July 22, 2009 6:39 AM
    • Marked as answer by Harry Zhu Thursday, July 23, 2009 9:33 AM
    Thursday, July 16, 2009 1:46 AM

All replies

  • Can you show us the relevant code? Or why can't you simply use one of the existing collection classes like List<user>, that way LINQ certainly can easily create one with the ToList() method.

    MVP XML My blog
    • Proposed as answer by Harry Zhu Wednesday, July 22, 2009 6:39 AM
    • Marked as answer by Harry Zhu Thursday, July 23, 2009 9:33 AM
    Wednesday, July 15, 2009 4:53 PM
  • You can't cast one collection type into another (in this case, presumably IQueryable<User> or EntitySet<User> into MyCollection) - you have to repopulate it:

      var result = new MyCollection (db.Users.Where (u => u.Name.StartsWith ("A"));

    Then write a constructor on your collection class to accept IEnumerable<User>

      class MyCollection
      {
         public MyCollection (IEnumerable<User> users)
         {
            foreach (User u in users) Add (u);
         }
      }

    Joe

    Write LINQ queries interactively - www.linqpad.net
    • Proposed as answer by Harry Zhu Wednesday, July 22, 2009 6:39 AM
    • Marked as answer by Harry Zhu Thursday, July 23, 2009 9:33 AM
    Thursday, July 16, 2009 1:46 AM