2011-12-09 76 views

回答

0

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

的问题,我不完全理解,是这一行:

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()不支持?

+0

试试'contacts.Count()'这应该会给你返回的联系数量。 – jacobappleton

2

您需要像这样:

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

希望有所帮助。

+0

而不是使用.Substring,使用.EndsWith会不会更容易/更高效?正如在'where c.emailaddress1.EndsWith(“@ domain.com”)'? –

+0

效率明智我会说他们很相似:http://msdn.microsoft.com/en-us/library/system.string.endswith%28v=vs.71%29.aspx但我会承认'EndsWith'稍微简单一些。 – jacobappleton

相关问题