使用LINQ更新CRM 2011中的记录

时间:2011-08-28 18:35:09

标签: linq dynamics-crm crm dynamics-crm-2011

我正在尝试使用LINQ更新OpportunitySet中的记录。现在我将所有数据都拉下来,它们正在数据绑定到网格中的字段。这就是我到目前为止所做的:

        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();

此代码将我的所有信息都删除,然后使用以下内容将其分配给控件:

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

但是现在我试图这样做,当你点击一个保存按钮时,它将保存文本框中的任何更改。我如何使用LINQ做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果你还没有坚持某种架构,我建议你使用像MVVM这样的经过验证的模式,或者看看微软的MVC项目。您正在使用由Linq生成的匿名类与UI进行通信,因此UI与您的数据访问层之间存在直接链接。换句话说,没有关注点的分离。这是非常有问题的。例如,您在哪里编写验证逻辑?这可能会发展成为具有大量复制和粘贴编程的大型整体结构。

它总是需要付出很多努力和学习,因此,时间来建立一个基本的架构,但推迟它将最终耗时,而且肯定不那么有趣!所以我希望时间和先决条件允许你走这条路。在我的团队中,即使对于试点项目,我也鼓励人们总是首先决定架构,因为试点项目有一种方法可以发展成为你永远无法摆脱的讨厌的持续项目。