Lambda表达式用。凡使用条款包含 [英] Lambda expression with .Where clause using Contains

查看:182
本文介绍了Lambda表达式用。凡使用条款包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当连接到CRM 2013有没有创造一个lambda表达式,得到谁的GUID在列表中的实体聪明的方式。



在哪里此代码中断条款并给出了错误:




无效',其中'状态。 。一个实体成员调用一个无效的属性或方法




代码:

 私人列表< UserInformationProxy> GetContactsFromGuidList(列表<&的Guid GT; contactList)
{
VAR的结果= _serviceContext.ContactSet
。凡(X => contactList.Contains((GUID)x.ContactId))//这一行符
。选择(X =>新建UserInformationProxy()
{
全名= x.FullName,
n = x.ContactId
})
.Distinct()
.ToList< UserInformationProxy>();

返回结果;
}

//返回类
公共类UserInformationProxy
{
公众的Guid?标识{搞定;组; }
公共字符串全名{获得;组; }
公共字符串域名{搞定;组; }
}

目前我所得到一切从ContactSet触点解决这个和整理我想在我的代码循环的人。这工作,但速度很慢,因为我需要让所有的10000联系人不是发送40 Im的的GUID真正感兴趣到SQL服务器。


解决方案

QueryExpressions支持在运营商,所以这应该只是罚款:



<预类=郎-CS prettyprint-覆盖> 私人列表< UserInformationProxy> GetContactsFromGuidList(列表<&的Guid GT; contactList)
{
变种QE =新QueryExpression(Contact.EntityLogicalName);
qe.ColumnSet =新ColumnSet(全名,的ContactID)
qe.Criteria.AddCondition(的ContactID,ConditionOperator.In,list.Cast&所述;对象>()ToArray的()) ;
qe.Distinct = TRUE;

VAR的结果= service.RetrieveMultiple(QE).Entities.Select(E => e.ToEntity<联系与GT;())。
选择(X =>新建UserInformationProxy()
{
全名= x.FullName,
n = x.ContactId
});

返回结果;
}

在一个侧面说明,每个联系人都必须有一个ID,它是不是空,所以没有必要进行检查。


When connecting to CRM 2013 is there a smart way to create a lambda expression that gets the entities who's GUID are in a List.

This code breaks on the Where clause and gives the error:

Invalid 'where' condition. An entity member is invoking an invalid property or method.

Code:

    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; }
    }

Currently I'm solving this by getting all the contacts from the ContactSet and sorting out the ones I want with a loop in my code. This works, but is quite slow as I need to get all 10000 contacts instead of sending the Guids of the 40 Im actually interested in to the SQL server.

解决方案

QueryExpressions support an In operator, so this should work just fine:

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;
}

On a side note, every Contact has to have an Id that is not empty, so there is no need to check for it.

这篇关于Lambda表达式用。凡使用条款包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆