开发者

Update Records in CRM 2011 using LINQ

I am trying to Update records in the OpportunitySet using LINQ. Right now I am pulling all the data down and they are being Databound to fields in a Grid. This is what I have so far:

        Uri organizationUri = new Uri("http://servername/XRMServices/2011/Organization.svc");
    Uri homeRealmUri = null;
    ClientCredentials credentials = new ClientCredentials();
    credentials.Windows.ClientCredential = new System.Net.NetworkCredential("user", "pass", "domain");
    OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
    // Get the IOrganizationService
    IOrganizationService orgService = (IOrganizationService)orgProxy;
    //Get OrganizationServiceContext -the organization service context class implements the IQueryable interface and
    //a .NET Language-Integrated Query (LINQ) query provider so we can write LINQ queries against Microsoft Dynamics CRM data.
    OrganizationServiceContext orgServiceContext = new OrganizationServiceContext(orgService);


    var linqQuery = (from r in orgServiceContext.CreateQuery("opportunity")
                     join a in orgServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals a["accountid"]
                     join c in orgServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"]
                     where ((EntityReference)r["new_channelpartner"]).Id.Equals(new Guid("c55c2e09-a3be-e011-8b2e-00505691002b"))
                     select new
                     {
                         OpportunityId = !r.Contains("opportunityid") ? string.Empty : r["opportunityid"],
                         CustomerId = !r.Contains("customerid") ? string.Empty : ((EntityReference)r["customerid"]).Name,
                         Priority = !r.Contains("opportunityratingcode") ? string.Empty : r.FormattedValues["opportunityratingcode"],
                         ContactName = !r.Contains("new_contact") ? string.Empty : ((EntityReference)r["new_contact"]).Name,
                         Source = !r.Contains("new_source") ? string.Empty : r["new_source"],
                         CreatedOn = !r.Contains("createdon") ? string.Empty : r["createdon"],
                         State = !a.Contains("address1_stateorprovince") ? string.Empty : a["address1_stateorprovince"],
                         Zip = !a.Contains("address1_postalcode") ? string.开发者_高级运维Empty : a["address1_postalcode"],
                         Eval = !r.Contains("new_colderevaluation") ? string.Empty : r.FormattedValues["new_colderevaluation"],
                         EvalVal = !r.Contains("new_colderevaluation") ? string.Empty :((OptionSetValue)r["new_colderevaluation"]).Value.ToString(),
                         DistributorName = !r.Contains("new_channelpartner") ? string.Empty : ((EntityReference)r["new_channelpartner"]).Name,
                         ContactStreetAddress = !c.Contains("address1_line1") ? string.Empty : c["address1_line1"],
                         ContactCity = !c.Contains("address1_city") ? string.Empty : c["address1_city"],
                         ContactState = !c.Contains("address1_stateorprovince") ? string.Empty : c["address1_stateorprovince"],
                         ContactZip = !c.Contains("address1_postalcode") ? string.Empty : c["address1_postalcode"],
                         ContactPhone = !c.Contains("telephone1") ? string.Empty : c["telephone1"],
                         ContactMobilePhone = !c.Contains("mobilephone") ? string.Empty : c["mobilephone"],
                         ContactEmail = !c.Contains("emailaddress1") ? string.Empty : c["emailaddress1"],
                         Notes = !r.Contains("new_rsmnotes") ? string.Empty : r["new_rsmnotes"],
                         EstimatedCloseDate = !r.Contains("estimatedclosedate") ? string.Empty : r["estimatedclosedate"],
                         MaturityValue = !r.Contains("estimatedvalue") ? string.Empty : ((Money)r["estimatedvalue"]).Value.ToString()
                     });

    grdLeadList.DataSource = linqQuery;

    grdLeadList.DataBind();

This code pulls all my information down and then assign it to controls using something like:

<asp:TestBox ID="Label1" runat="server" Text='<%# DataBinder.Eval( Container, "DataItem.ContactEmail" ) %>' />

But now I am trying to make it so when you click a save button it will save any changes in the textbox. How would I do that using LINQ?

Thanks!


If you're not stuck to some architecture yet, I would suggest you use a proven pattern like MVVM or take a look at Microsoft's MVC project. You are using an anonymous class genereated by Linq to communicate with the UI, so there is a direct link between UI and your data access layer. In other words, there is no separation of concerns. This is very problematic. For instance, where do you program your validation logic? This will probably develop into large monoliths with lots of copy-and-paste programming.

It always takes a lot of effort and learning and, thus, time to set up a basic architecture, but postponing it will in the end be far more time-consuming and certainly less fun! So I hope time and prerequisites allow you to go this path. In my team, even for pilot projects I encourage people to always decide on architecture first because pilot projects have a way of developing into nasty persistent projects that you never get rid of.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜