【问题标题】:CRM 2011 - Quickest Lookup of Account by Email Address DomainCRM 2011 - 通过电子邮件地址域快速查找帐户
【发布时间】:2012-02-21 10:11:37
【问题描述】:

我正在寻找最快的方法来返回一组帐户,这些帐户的联系人的电子邮件地址与 CRM 2011 中的域名匹配。我正在使用早期绑定实体。我认为 Linq 查询将是最简单、最快的,但不知道从哪里开始。谢谢。

【问题讨论】:

    标签: c# linq dynamics-crm


    【解决方案1】:

    你需要这样的东西:

    var query = (
        from c in ctx.contacts
        where c.emailaddress1.Substring(c.emailaddress1.IndexOf('@')) == "@domain.com"
        && c.statuscode == 0
        select c);
    

    这是假设您已经创建了早期绑定类并设置了数据上下文。

    此链接提供了大量信息,让您了解实际运行上述 Linq 代码所需的点:http://sandrinodimattia.net/blog/post/Early-binding-tips-and-tricks-for-Dynamics-CRM-2011.aspx

    希望对您有所帮助。

    【讨论】:

    • 不使用 .Substring,使用 .EndsWith 不是更容易/更有效吗?如where c.emailaddress1.EndsWith("@domain.com")?
    • 效率方面我会说它们非常相似:msdn.microsoft.com/en-us/library/… 但我承认EndsWith 稍微容易一些。
    【解决方案2】:

    使用@jacobappleton start,我收到一个错误“无效的'where'条件。实体成员正在调用无效的属性或方法。”

    我不完全理解的问题在于这一行:

    where c.EMailAddress1.Substring(c.EMailAddress1.IndexOf('@')) == email.Substring(email.IndexOf('@')
    

    虽然不太准确,但我已将其替换为以下内容。在查询之前定义域。

    where c.EMailAddress1.Contains(domain)
    

    最终结果:

    public Account GetAccount(string email)
    {
        var context = new ServiceContext(_service);
        var domain = email.Substring(email.IndexOf('@'));
        var contacts = from c in context.ContactSet
                       where c.EMailAddress1.Contains(domain)
                       where c.StateCode == ContactState.Active
                       where c.ParentCustomerId != null
                       select c;
    
        return RetrieveEntity(Account.EntityLogicalName, contacts.First<Contact>().ParentCustomerId.Id, new ColumnSet(true)).ToEntity<Account>();
    }
    

    相关,如何查看返回的记录数。 contacts.Any() 不受支持?

    【讨论】:

    • 试试contacts.Count()这应该会给你返回的联系人数量。
    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多