I have an entity framework class (Document) with a child (Provider) that has an array of objects (Documents). I want to pull my Document
class and include it's Provider
child but not include the Provider's array of Document
objects. I can't remove the documents property from Provider because I do need the documents array at other times. How do I pull the documents with providers, but exclude each providers documents array with linq?
Here's my class structure:
Document {
public string DocumentName { get; set; }
public virtual Provider Provider { get; set; }
}
Provider {
public string Name { get; set; }
public virtual ICollection<Document> Documents { get; set; }
}
I'm trying to get my document objects using linq like this:
db.Documents.Where(x => x.Id == Id)
.Include(x => x.Provider)
.ToList();
And here's what the result looks like:
[{
'DocumentName': 'document_one.pdf',
'Provider': {
'Name': 'Jim Smith',
'Documents': [
{
'DocumentName': 'document_one.pdf'
},
{
'DocumentName': 'document_two.pdf'
},
]
}
}]
So how do I accomplish getting this exact result set, just without the Provider.Documents array being populated? Again, I can't remove the Documents property from the Provider class because I need it at other times.