0

I would like to allow reparenting in Fluent NHibernate.
Here I read that I have to create two classes which inherit from IUserCollectionType and PersistentGenericBag<MyClass>. If I change Box from the example to my own classname, everything works fine - except one thing: In the class which inherits from PersistentGenericBag<MyClass> I can't override GetOrphans() because Cast() is not defined for an ICollection.
This is what I would like to have:

public override ICollection GetOrphans(object snapshot, string entityName)
{
    var orphans = base.GetOrphans(snapshot, entityName)
        .Cast<MyCalss>()
        .Where(b => ReferenceEquals(null, b.CurrentStorage))
        .ToArray();

    return orphans;
}

How can I use Cast on an ICollection?

Edit
This is the exact error I get (unfortunatly it's german): Fehler 1 "System.Collections.Generic.ICollection<T>" enthält keine Definition für "Cast", und es konnte keine Erweiterungsmethode "Cast" gefunden werden, die ein erstes Argument vom Typ "System.Collections.Generic.ICollection<T>" akzeptiert. (Fehlt eine Using-Direktive oder ein Assemblyverweis?)

Community
  • 1
  • 1
Christopher
  • 2,005
  • 3
  • 24
  • 50
  • Do you have the necessary `using System.Linq;` in your file? What is the exact error message what you get? – nemesv Nov 01 '12 at 13:21
  • Thank you! Now it works! I thought Visual Studio would tell me if I have to add an using directive, but it didn't. I added `using System.Linq;` an now I'm able to access `Cast()` Could you please post your hint as an answer? Than I could accept it ;) – Christopher Nov 01 '12 at 13:26

1 Answers1

1

Cast<T> is an extension method living in the System.Linq namespace.

And to use an extension method you need to add it's namespace with an using directive.

So just add the following using to your file and it should work:

using System.Linq;
nemesv
  • 138,284
  • 16
  • 416
  • 359