Search code examples
arrayslinqc#-4.0sortingc1-cms

Composite C1 How would I rewrite this Sql update statement to work in c#?


I have a piece of code in an image sortable grid which sends back a resulting string array of integers based on the user's new sort order for 'propid':

 { 'imgid': '4,2,3,5,6,7,8,9,1','propid':'391' }

The above shows 9 images on the screen. The db image table has both an image id (imgid) field and a sort sequence field (orderseq). I am using a custom namespace datatype:

< connection.Get< ALocal.propimage >() 

like all datatype connections in C1.

In direct SQL I would write this:

string []q = imgid.Split(',');
string qry="";
for (int i = 0; i < q.Length; i++)
{
  qry += "update ALocal_propimage set propimage_orderseq="+(i+1)+"  where prop_id="+propid+" and  propimage_id="+q[i]+"  ;";
}
sqlHelper obj = new sqlHelper();
obj.ExecuteNonQuery(qry);
return "Record Updated";

How does this convert to writing it using c# into Composite's C1 CMS 'Updating Multiple Data' method as I keep failing at it?

The C1 site 'Updating Multiple Data' method rudimentary example is:

using  (DataConnection connection = new DataConnection())
{
 var myUsers = connection.Get<Demo.Users>().Where (d => d.Number < 10).ToList();
 foreach  (Demo.Users myUser in myUsers)
{
  myUser.Number += 10;
}   
connection.Update<Demo.Users>(myUsers);
} 

Any help would be really appreciated.


Solution

  • You would need to split your update code into a get and a update, to let C1 know exactly which entity you would like to update. So something like this

    for (int i = 0; i < q.Length; i++)
    {
      var propimages = connection.Get<ALocal.propimage>().Where(o => o.PropId = propid && p.PropImageId = q[i]);
      foreach (var o in propimages) 
      {
         o.OrderSeq = i + 1;
      }
    
      connection.Update(propimages);
    }