Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq-to-SQL Table Coalesce
    text
    copied!<p>Let's say I have three tables:</p> <p>User</p> <pre> ID | Name =========== 1 | UserA ----------- 2 | UserB ----------- </pre> <p>Setting</p> <pre> ID | Name | Default ========================= 1 | SettingA | ADefault ------------------------- 2 | SettingB | BDefault ------------------------- </pre> <p>And</p> <p>UserSetting</p> <pre> UserID | SettingID | Value ================================ 1 | 1 | Alice -------------------------------- 1 | 2 | Bob -------------------------------- 2 | 1 | AOverride -------------------------------- </pre> <p>When I create my dbml file, it appropriately links User to <code>UserSetting</code> and <code>UserSetting</code> to <code>Setting</code> based on the foreign keys set up in the database.</p> <p>What I'm wondering is if its possible to coalesce back from <code>UserSetting</code> to the <code>Setting</code> table if the user hasn't specifically overridden the value in a way that makes sense.</p> <p>Specifically, I'm looking for the following pseudo-code:</p> <pre><code>var user = MyDataContext.Users.SingleOrDefault(u =&gt; u.ID == 2); foreach(var setting in user.UserSettings) { Console.Writeline(setting.ID + "|" + setting.Value); } </code></pre> <p>To output something like this:</p> <pre> 1 | AOverride 2 | BDefault </pre> <p>Without modification, user.UserSettings will only contain the values that have specifically been overridden, so it will only return:</p> <pre> 1 | AOverride </pre> <p>Any ideas? Or could someone with more rep please help me rephrase this, since it probably isn't exactly clear? :)</p>
 

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