Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ - Place part of a query that is always the same in a separate class and use in other linq queries
    primarykey
    data
    text
    <p><strong>Current situation</strong></p> <p>Currently I have this <code>Linq</code> query:</p> <pre><code>return from function in this.context.Functions join fp in this.context.FunctionParameters on function.FunctionID equals fp.FunctionID into functionParameters from functionParameter in functionParameters.DefaultIfEmpty() join d in this.context.Descriptions on new { ID = (int)function.FunctionID, languageID = languageID, originID = (byte)origin } equals new { ID = d.ValueID, languageID = d.LanguageID, originID = d.OriginID } into entityWithDescription from x in entityWithDescription.DefaultIfEmpty() select new FunctionDTO() { Function = function, Description = x }; </code></pre> <p>This returns the functions with their parameters and the specific descriptions. So, a select with two left outer joins. This is all good and works.</p> <p><strong>The problem</strong></p> <p>I have multiple objects that have a description. The description table has no relationship with these objects (so no FK). So there is a part of the above query that is <strong>always</strong> the same, namely the join query to the description table:</p> <pre><code>join d in this.context.Descriptions on new { ID = (int)function.FunctionID, languageID = languageID, originID = (byte)origin } equals new { ID = d.ValueID, languageID = d.LanguageID, originID = d.OriginID } into entityWithDescription </code></pre> <p>The variables <code>languageID</code> and <code>origin</code> are two parameters that are passed on with the method. The <code>FunctionID</code> is a property in my <code>Function</code> class, ie a property in my <code>entity model</code>. So that is a </p> <pre><code>public partial class Function { public byte FunctionID { get; set; } /** Other properties **/ } </code></pre> <p><strong>My question</strong></p> <p>Is it possible to create a separate class with the part of the <code>linq</code> query that is always the same? So that I don't have to duplicate the same code all over again? </p> <p><strong>What I already tried</strong></p> <pre><code>var query = from function in this.context.Functions join fp in this.context.FunctionParameters on function.FunctionID equals fp.FunctionID into functionParameters from functionParameter in functionParameters.DefaultIfEmpty() select function; var testResult = this.context.Descriptions.GetDescriptionsByJoin(query, languageID, origin); </code></pre> <p>And the duplicate code in a separate class:</p> <pre><code>public static IQueryable&lt;IEnumerable&lt;Description&gt;&gt; GetDescriptionsByJoin( this IDbSet&lt;Description&gt; descriptions, IQueryable&lt;ITranslatable&gt; query, byte languageID, OriginEnum origin) { return from q in query join d in descriptions on new { ID = q.ValueID, languageID = languageID, originID = (byte)origin } equals new { ID = d.ValueID, languageID = d.LanguageID, originID = d.OriginID } into entityWithDescription select entityWithDescription; } </code></pre> <p>But this gave me the following error:</p> <pre><code>The specified type member is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported </code></pre> <p>I know that I get this error because I use my 'valueID' as a parameter in my join statement and that variable can't be found in my entity model (valueID is a property in an interface 'ITranslatable' that all my classes that have descriptions will implement).</p> <p>Thanks in advance!</p> <p>Greetings Loetn</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.
 

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