locked
Updating a subset of fields using the CRM LINQ provider RRS feed

  • Question

  • Does the linq provider support updating an entity with only some field values set?

    I'm using the linq provider to fetch a subset of fields for an entity and projecting it to an anonymous type like so:

    var query = from c in mycontext.myentityset
                      where c.isactive == true
                      select new { Id=c.Id, Status=c.status, DeactivateOndate = c.deactivateondate};

    I then iterate over the results and need to update just the status like so:
    foreach(var obj in query)
    {
      if(obj.deactivateondate <= DateTime.UtcNow)
      {
       //create the original entity type instance and use the fields of the anonymous type to set the
       //fields we want to update
         var myentityInstance = new myentity{Id = c.Id, status = statusenum.basicMembership}
         context.AddObject(myentityInstance)
         context.saveChanges()
      }
    }

    Note, that I'm newing up a brand new instance of "myentity", setting the Id and updating just the
    fields I need to update.

    Is this supported and will the LINQ provider use the "Id" to match up the entity instance and
    update ONLY THE FIELDS that I have set in the for loop above. I need to ensure that the fields I dont touch are left untouched and have the original values retained.

    This post leads me to believe that I'm on the right pathbut just wanted confirmation

    http://social.microsoft.com/Forums/en-US/crmdevelopment/thread/c3725efd-3eb9-4d75-b542-6982206cf6a4/

    Thanks


    -Abhijeet


    • Edited by Abhijeet P Saturday, September 22, 2012 8:18 PM updates
    Saturday, September 22, 2012 8:11 PM

Answers

  • Hi Abhijeet,

    You are spot on. When you use select new - it is called a 'projection' and the CRM LINQ provider interprets this as creating a QueryExpression that only contains the fields that you reference. You end up with an entity with all other attributes not loaded into the 'Attributes' collection (rather than having null values loaded). If you did an update on your projection, you would only update the attributes that you retrieved.

    Your code essentially does another projection with only the status attribute - so when you update that, only the status will be sent in the update statement since no other attributes are present in the attributes collection.

    hth,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Sunday, September 23, 2012 7:50 PM
    Answerer

All replies

  • Hi Abhijeet,

    You are spot on. When you use select new - it is called a 'projection' and the CRM LINQ provider interprets this as creating a QueryExpression that only contains the fields that you reference. You end up with an entity with all other attributes not loaded into the 'Attributes' collection (rather than having null values loaded). If you did an update on your projection, you would only update the attributes that you retrieved.

    Your code essentially does another projection with only the status attribute - so when you update that, only the status will be sent in the update statement since no other attributes are present in the attributes collection.

    hth,

    Scott


    Scott Durow
    Read my blog: www.develop1.net/public
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Sunday, September 23, 2012 7:50 PM
    Answerer
  • Perfect..exactly what I needed...

    Thanks.


    -Abhijeet

    Tuesday, September 25, 2012 2:56 PM