4

CRM 2013 に接続するときに、リストにある GUID のエンティティを取得するラムダ式を作成するスマートな方法があります。

このコードは Where 句で壊れ、エラーが発生します。

'where' 条件が無効です。エンティティ メンバーが無効なプロパティまたはメソッドを呼び出しています。

コード:

    private List<UserInformationProxy> GetContactsFromGuidList(List<Guid> contactList)
    {
        var result = _serviceContext.ContactSet
            .Where(x=> contactList.Contains((Guid) x.ContactId)) // this line breaks
            .Select(x => new UserInformationProxy()
            {
                FullName = x.FullName,
                Id = x.ContactId
            })
            .Distinct()
            .ToList<UserInformationProxy>();

        return result;
    }

    // return class
    public class UserInformationProxy
    {
        public Guid? Id { get; set; }
        public string FullName { get; set; }
        public string DomainName { get; set; }
    }

現在、ContactSet からすべての連絡先を取得し、コード内のループで必要なものを整理することで、これを解決しています。これは機能しますが、実際に関心のある 40 の Guid を SQL サーバーに送信する代わりに、10000 の連絡先をすべて取得する必要があるため、非常に遅くなります。

4

2 に答える 2

5

QueryExpressions は In 演算子をサポートしているため、これは問題なく機能するはずです。

private List<UserInformationProxy> GetContactsFromGuidList(List<Guid> contactList)
{
    var qe = new QueryExpression(Contact.EntityLogicalName);
    qe.ColumnSet = new ColumnSet("fullname", "contactid")
    qe.Criteria.AddCondition("contactid", ConditionOperator.In, list.Cast<Object>().ToArray());
    qe.Distinct = true;

    var results = service.RetrieveMultiple(qe).Entities.Select (e => e.ToEntity<Contact>()).
        Select(x => new UserInformationProxy()
        {
            FullName = x.FullName,
            Id = x.ContactId
        });

    return results;
}

ちなみに、すべての連絡先には空でない ID が必要なので、確認する必要はありません。

于 2014-01-13T14:01:13.857 に答える