Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to Entities (EF 4.0) and group 2 unrelated tables into an aggregated object
    primarykey
    data
    text
    <p>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..)</p> <p>Table Document: DocId, Name</p> <p>Table Message: MessageId, DocId, Value</p> <p>One document object can have zero or more Message objects</p> <p>I want to write a query that returns a list of an aggregated object called DocumentWithMessages:</p> <pre><code>public class DocumentWithMessages { Document Document {get;set;} List&lt;Message&gt; Messages {get;set;} } </code></pre> <p>The query should return all Documents (with or without a list of Messages)</p> <p>I started to write such a method but it gives me an exception:</p> <pre><code>public ICollection&lt;DocumentWithDocumentSplitMessages&gt; GetSplitJobsWithMessages() { var messages = (from dm in ObjectContext.DocumentSplitMessages select dm).ToList(); var jobs = from d in ObjectContext.Documents let msgs = messages.Where(dm =&gt; dm.DocID == d.DocID).ToList() select new DocumentWithDocumentSplitMessages() { Document = d, DocumentSplitMessages = msgs }; return jobs.ToList(); } </code></pre> <p>The exception is:</p> <p>System.NotSupportedException : LINQ to Entities does not recognize the method 'System.Collections.Generic.List<code>1[BraArkivDataModel.DocumentSplitMessage] ToList[DocumentSplitMessage](System.Collections.Generic.IEnumerable</code>1[BraArkivDataModel.DocumentSplitMessage])' method, and this method cannot be translated into a store expression.</p> <p>How do I write such a query?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload