views:

72

answers:

3

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> to System.Collection.Generic.List<Child>.

How can I write that LINQ query to return a List?

Best regards,

OKB

+6  A: 

result is a list of anonymous types, each with a member (children) that is an enumerable set of Child records. You should be able to use SelectMany here:

var list = (from item in result
            from child in item.children
            select child).ToList();

or (identical):

var list = result.SelectMany(item => item.children).ToList();
Marc Gravell
+2  A: 
var result = (from dc in _context.DocClasses
             join d in _context.Documents
             on dc.DocClassID equals d.DocClassID
             where dc.DocClassID != 0 && docIds.Contains(d.DocID)
             let 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
                                              }
              select children).SelectMany(c=>c).ToList();
Hasan Khan
Thank you for your quick reply!
OKB
+2  A: 

You are returning an anonymous type when you say select new { children ...

Carlos Muñoz