Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, first to directly answer your question (but read on for words of caution ;)), there is a way, albeit a finicky one, to do what you want.</p> <pre><code>// IQueryable&lt;Customer&gt; L2S query definition, db is DataContext (AdventureWorks) var cs = from c in db.Customers select c; // extract command and append your stuff DbCommand dbc = db.GetCommand(cs); dbc.CommandText += " WHERE MiddleName = 'M.'"; // modify command and execute letting data context map it to IEnumerable&lt;T&gt; var result = db.ExecuteQuery&lt;Customer&gt;(dbc.CommandText, new object[] { }); </code></pre> <p>Now, the caveats.</p> <ol> <li>You have to know which query is generated so you would know how to modify it, this prolongs development. </li> <li>It falls out of L2S framework and thus creates a possible gaping hole for sustainable development, if anyone modifies a Linq it will hurt.</li> <li>If your Linq causes parameters (has a where or other extension causing a WHERE section to appear with constants) it complicates things, you'll have to extract and pass those parameters to ExecuteQuery</li> </ol> <p>All in all, possible but very troublesome. That being said you should consider using .Where() extension as <a href="https://stackoverflow.com/users/51/yaakov-ellis">Yaakov</a> suggested. If you want to centrally controll security on object level using this approach you can create an extension to handle it for you</p> <pre><code>static class MySecurityExtensions { public static IQueryable&lt;Customer&gt; ApplySecurity(this IQueryable&lt;Customer&gt; source) { return source.Where(x =&gt; x.MiddleName == "M."); } } //... // now apply it to any Customer query var cs = (from c in db.Customers select c).ApplySecurity(); </code></pre> <p>so if you modify ApplySecurity it will automatically be applied to all linq queries on Customer object.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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