I have 2 tables (Document and Message) that have no foreign key relation, but the table Message have the id column from Document as a column (bad design, I know..)
Table Document:
DocId,
Name
Table Message:
MessageId,
DocId,
Value
One document object can have zero or more Message objects
I want to write a query that returns a list of an aggregated object called DocumentWithMessages:
public class DocumentWithMessages
{
Document Document {get;set;}
List<Message> Messages {get;set;}
}
The query should return all Documents (with or without a list of Messages)
I started to write such a method but it gives me an exception:
public ICollection<DocumentWithDocumentSplitMessages> GetSplitJobsWithMessages()
{
var messages = (from dm in ObjectContext.DocumentSplitMessages select dm).ToList();
var jobs = from d in ObjectContext.Documents
let msgs = messages.Where(dm => dm.DocID == d.DocID).ToList()
select new DocumentWithDocumentSplitMessages()
{
Document = d,
DocumentSplitMessages = msgs
};
return jobs.ToList();
}
The exception is:
System.NotSupportedException : LINQ to Entities does not recognize the method ‘System.Collections.Generic.List1[BraArkivDataModel.DocumentSplitMessage] ToList[DocumentSplitMessage](System.Collections.Generic.IEnumerable1[BraArkivDataModel.DocumentSplitMessage])’ method, and this method cannot be translated into a store expression.
How do I write such a query?
I think you can use an approach similar to what you tried but using the IQueryable on the let clause: