-5

I have a problem with the method where and the generic list.

    public class TestList : List<Test2>
    {
        public TestList getWork()
        {
        return (TestList)this.Where(a => a.ID=10);
        }
    }

I have the error follow:Unable to cast object of type 'WhereListIterator`1[Test2]' to type 'TestList'.

Example 2 , why is not a good idea use code like this:

public class Parent : List<Child>
{
    public Parent GetChildOf(Child child)
    {
        ...
        return childofchild;
    }
    public Parent GetParentOf(Child child)
    {
    ...
    }
}

in code in control .net I use my class like this:

parents.GetParentOf(childselected).GetParentOf(otherchild);....

Thanks for your help.

Zoners
  • 123
  • 1
  • 6
  • 11
  • 3
    The error seems pretty self explanatory. What don't you understand about it? – Servy Mar 24 '15 at 16:31
  • 4
    On a site note, you almost certainly shouldn't be extending `List`. You probably just want to compose it instead. – Servy Mar 24 '15 at 16:31
  • 1
    Building on @Servy's point, [here is a relevant question](http://stackoverflow.com/questions/21692193/why-not-inherit-from-listt) talking about reasons for not extending `List` – James Thorpe Mar 24 '15 at 16:35
  • 1
    As an added bonus, no matter the conversion that would never compile since the method is static and `this` certainly isn't. – J. Steen Mar 24 '15 at 16:39
  • @J.Steen it's correct, it's a error in my example, it's not a function static, I update my post, thanks – Zoners Mar 25 '15 at 10:44

1 Answers1

1

Even though a TestList is a List<Test2>, that doesn't mean that List<Test2>s are all TestLists. You'll need to create a new instance of your class, rather than just trying to cast the LINQ results.

Try this:

var testList = new TestList();
testList.AddRange(this.Where(a => a.ID=10));
return testList;

Servy's comment is correct, though, that it's probably a bad idea to create your own collection type that extends List<>. If you want to have a class that includes a bunch of Test2s along with other information, it's best to create a class that has those pieces as separate properties:

public class TestList
{
    public string SomeRandomInfo {get;set;}
    public List<Test2> Test2s {get;set;}
}
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Thanks, your example is correct and it's my solution, but I update my post with example2, why is not a good idea use my example2 like this? – Zoners Mar 25 '15 at 10:34
  • @Zoners: There are too many reasons to list in this comment. If I were to ask you what it means to be a parent, would you say that it means you *are* a list of children? That's not how you think about real-life scenarios, so why are you modeling your code in that way? Also, you can only extend one class: if you want to have both `Human` and `Animal` objects that are both `Parent`s, how would you do that? If you're interested in learning more, start by reading the answers in the question that James Thorpe linked to in his comment on your question. Then research "Composition over Inheritance." – StriplingWarrior Mar 25 '15 at 16:31