Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# CRM 2011-按电子邮件地址域快速查找帐户_C#_Linq_Dynamics Crm - Fatal编程技术网

C# CRM 2011-按电子邮件地址域快速查找帐户

C# CRM 2011-按电子邮件地址域快速查找帐户,c#,linq,dynamics-crm,C#,Linq,Dynamics Crm,我正在寻找最快的方式返回一组帐户,这些帐户的联系人的电子邮件地址与CRM 2011中的域名匹配。我使用的是早期绑定实体。我认为Linq查询最简单、最快,但不确定从哪里开始。谢谢。您需要这样的东西: var query = ( from c in ctx.contacts where c.emailaddress1.Substring(c.emailaddress1.IndexOf('@')) == "@domain.com" && c.statuscode

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

您需要这样的东西:

var query = (
    from c in ctx.contacts
    where c.emailaddress1.Substring(c.emailaddress1.IndexOf('@')) == "@domain.com"
    && c.statuscode == 0
    select c);
这是假设您已经创建了早期绑定类并设置了数据上下文

此链接提供了相当多的信息,可以帮助您达到实际运行上述Linq代码所需的程度:


希望能有所帮助。

使用@jacobappleton start,我得到一个错误“Invalid'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>();
}
公共帐户GetAccount(字符串电子邮件)
{
var-context=新服务上下文(_服务);
var domain=email.Substring(email.IndexOf('@');
var contacts=来自context.ContactSet中的c
其中c.EMailAddress1.Contains(域)
其中c.StateCode==ContactState.Active
其中c.ParentCustomerId!=null
选择c;
返回RetrieveEntity(Account.EntityLogicalName,contacts.First().ParentCustomerId.Id,new ColumnSet(true)).ToEntity();
}

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

而不是使用.Substring,使用.EndsWith是否更容易/更有效?在
中,c.emailaddress1.EndsWith(@domain.com”)
?效率方面,我想说它们非常相似:但我承认
EndsWith
稍微容易一些。试试
contacts.Count()
这应该会告诉你返回的联系人数量。