Note that there are some explanatory texts on larger screens.

plurals
  1. POCheck if value is null, and when it is, execute extra (sub)query
    primarykey
    data
    text
    <p>Is it possible to check for a null value in a LINQ query and when the value is null, that it executes an extra (sub)query, all at once?</p> <p><strong>Explanation</strong></p> <p>I have default buttons declared in my database, with default <em>descriptions</em>. A user can customize these buttons, and these settings are stored in the <code>ButtonLocations</code> table. Now, every button has a standard description and the user can edit this description. When the user edits the description, it is stored in the <code>Descriptions</code> table in my database. When I retrieve all buttons, I first check if a button has a specific description (in <em>buttonlocations</em>, with a left join). If this is not true (so null), I retrieve the default description. </p> <p>Currently I get all my entities with their description and after that I loop through all of them to check if the value is null. This results in multiple queries to the database.</p> <pre><code>var temp = (from bl in context.buttonLocations join b in context.Buttons on bl.ButtonID equals b.ButtonID into buttons from button in buttons.DefaultIfEmpty() join d in context.Descriptions on new { ID = bl.ButtonLocationID, langID = languageID, originID = descriptionOriginID } equals new { ID = d.ValueID, langID = d.LanguageID, originID = d.DescriptionOriginID } into other where bl.ButtonGroupID == buttonGroupId from x in other.DefaultIfEmpty() select new { Button = button, ButtonLocation = bl, Description = x }).ToList(); // Retrieve default descriptions if no specific one is set foreach (var item in temp) { if (item.Description == null) { item.Description = context.Descriptions .FirstOrDefault(x =&gt; x.ValueID == item.Button.ButtonID &amp;&amp; x.LanguageID == languageID &amp;&amp; x.DescriptionOriginID == (short)DescriptionOriginEnum.Button); } } </code></pre>
    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