Hi,
I have 2 tables (Document and DocumentClass) that have the following columns:
DocumentClass: DocClassID, Name, ParentID
Document: DocID, Name, DocClassID
The DocumentClass
table contains parent and child records and the relationship between a parent and a child is the ParentID column. A Document
record is linked with a child record in the DocumentClass
by the DocClassID foreign key.
Parent records in DocumentClass
have ParentID = 0 and Child records in the DocumentClass
have ParentID != 0
I want to retrieve a childs Name and that child's Parents Name from the DocumentClass
table.
I Have managed to create a function that does that for me. I send in a list of Document ids, find those DocumentClass
records (the child records) that the document is linked to and then find the parent to those child records. Then I put that information I want into a Child class.
public List<Child> GetDocClassInfo(List<int> docIds)
{
var result = from dc in _context.DocClasses
from d in _context.Documents
where dc.DocClassID == d.DocClassID
where dc.DocClassID != 0
where docIds.Contains(d.DocID)
select new
{
children = from p in _context.DocClasses
where dc.ParentID == p.DocClassID
select new Child
{
ChildId = dc.DocClassID,
ChildDocClassName = dc.DocClassName,
ParentId = p.DocClassID,
ParentDocClassName = p.DocClassName
}
};
return result.ToList();
}
My problem is that I want to have a List to return from the function, but the compiler doesn't like this at all. I get an error saying that
Cannot implicitly convert type
System.Collections.Generic.List``<AnonymousType#1>
toSystem.Collection.Generic.List<Child>
.
How can I write that LINQ query to return a List?
Best regards,
OKB