2011-12-09 41 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

相關問題