Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I finally figured out a way to do this. Basically, get the generated SQL from the Entity Framework, LINQ-to-SQL, or another ORM, then parse the WHERE clause. That way I don't have to parse the lambda manually. Then create an UPDATE clause from an anonymous type. The result looks like:</p> <pre><code>Update&lt;Task&gt;( new { UserID = 1, TaskCount = IncrementOf(1), SomeOtherField = DdNull } //Update w =&gt; w.Priority != "High" &amp;&amp; (w.Status != "Complete" || w.Status == null) //Where ); Delete&lt;Task&gt;(w =&gt; w.UserID == userID &amp;&amp; w.Status != "Complete"); </code></pre> <p>This allows me to update/delete values WITHOUT pulling them first. </p> <p>And the code for it looks like this...</p> <pre><code>protected void Update&lt;T&gt;(object values, Expression&lt;Func&lt;T, bool&gt;&gt; where) where T : class { Domain.ExecuteStoreCommand( "UPDATE {0} SET {1} WHERE {2};", GetTableString&lt;T&gt;(), GetUpdateClauseString(values), GetWhereClauseString(where) ); } protected string GetUpdateClauseString(object obj) { string update = ""; var items = obj.ToDictionary(); foreach (var item in items) { //Null if (item.Value is DBNull) update += string.Format("{0} = NULL", GetFieldString(item.Key)); //Increment else if (item.Value is IncrementExpression) update += string.Format("{0} = {0} + {1}", GetFieldString(item.Key), ((IncrementExpression)item.Value).Value.ToString()); //Decrement else if (item.Value is DecrementExpression) update += string.Format("{0} = {0} - {1}", GetFieldString(item.Key), ((DecrementExpression)item.Value).Value.ToString()); //Set value else update += string.Format("{0} = {1}", GetFieldString(item.Key), GetValueString(item.Value)); if (item.Key != items.Last().Key) update += ", "; } return update; } protected string GetWhereClauseString&lt;T&gt;(Expression&lt;Func&lt;T, bool&gt;&gt; where) where T : class { //Get query var query = ((IQueryable&lt;T&gt;)Domain.CreateObjectSet&lt;T&gt;()); query = query.Where(where); ObjectQuery queryObj = (ObjectQuery)query; //Parse where clause string queryStr = queryObj.ToTraceString(); string whereStr = queryStr.Remove(0, queryStr.IndexOf("WHERE") + 5); //Replace params foreach (ObjectParameter param in queryObj.Parameters) { whereStr = whereStr.Replace(":" + param.Name, GetValueString(param.Value)); } //Replace schema name return whereStr.Replace("\"Extent1\"", "\"Primary\""); } </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.
    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.
    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