C# 如何在Dynamics CRM 2011中查询(使用LINQ)格式化的值

C# 如何在Dynamics CRM 2011中查询(使用LINQ)格式化的值,c#,dynamics-crm-2011,C#,Dynamics Crm 2011,我正在使用LINQ从Microsoft Dynamics CRM Online检索帐户类型实体。我无法筛选列表中的特定格式化值。我有正确的值,但收到的记录为零。 我正在创建这样的连接: var connection = new CrmConnection("CRMOnline"); connection.ProxyTypesEnabled = true; CrmOrganizationServiceContext _context = new CrmOrganizationServiceCont

我正在使用LINQ从Microsoft Dynamics CRM Online检索帐户类型实体。我无法筛选列表中的特定格式化值。我有正确的值,但收到的记录为零。 我正在创建这样的连接:

var connection = new CrmConnection("CRMOnline");
connection.ProxyTypesEnabled = true;
CrmOrganizationServiceContext _context = new CrmOrganizationServiceContext(connection);
我试过:

List<Account> items = _context.CreateQuery<Account>()
                              .Where( c => ((OptionSetValue)c["new_accreditationstatus"]).Equals(7))
                              .ToList();
List items=\u context.CreateQuery()
其中(c=>((OptionSetValue)c[“新认证状态”)。等于(7))
.ToList();

List items=\u context.CreateQuery()
其中(c=>c.GetFormattedAttributeValue(“新认证状态”)==“7”
.ToList();

List items=\u context.CreateQuery()
.其中(c=>c[“新认证状态”]==“7”
.ToList();
最后一个on抛出System.Format异常

正常属性上的过滤器,即
。其中(c=>c.AccountNumber.StartsWith(“2010”))
工作得非常好。

编辑(尝试此操作):


另一个伟大的问题,但不幸的是,我认为这将代表另一个问题,它没有提到任何关于
FormattedValues
作为
Where
子句的允许使用,尽管它被允许作为
Select
子句中的一项

OptionSetValue
s的实际值存储在
StringMap
实体中,顺便说一句,您可以通过Linq访问
StringMap
实体。示例如下

//此查询获取此实体和字段的一个允许值。
var actualValue=\u context.CreateQuery(“stringmap”)
其中(x=>x.GetAttributeValue(“attributename”)==“新的认证状态”)
其中(x=>x.GetAttributeValue(“值”)==“7”)
.Where(x=>x.GetAttributeValue(“objecttypecode”)==Account.EntityTypeCode)
.Select(x=>x.GetAttributeValue(“值”))
.Single();
但是,尝试使用子查询和原始查询的版本(如下所示)在此基础上进行构建,会导致异常,如下所示

var actualValues = _context.CreateQuery("stringmap")
    .Where(x => x.GetAttributeValue<string>("attributename") == "new_accreditationstatus")
    .Where(x => x.GetAttributeValue<int>("objecttypecode") == Xrm.Account.EntityTypeCode);

// This (modified) query uses the StringMap values from the previous query in
// a subquery, linking to the int (attributevalue) value that
// new_accreditationstatus represents.
List<Account> items = _context.CreateQuery<Account>()
    .Where(c => actualValues
        .Where(x => x.GetAttributeValue<int>("attributevalue") == c.new_accreditationstatus.Value)
        .Select(x => x.GetAttributeValue<string>("attributevalue"))
        .Single() == "7")
    .ToList();

如果我能找到一种方法在一次查询中完成所有这些,我一定会编辑并重新发布。

在生成早期绑定的CRM文件时,您只能访问
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu以及创建
CrmOrganizationServiceContext
(通常称为
XrmServiceContext
)的早期绑定派生。您可以在早期绑定文件中看到可用的构造函数

因此,如果您事先知道
OptionSetValue
的(int)值(在本例中为7),您可以将该值用作
Where
子句中的参数之一,如您在其他地方所述:

.Where(c=>c.new\u authenticationstatus.Value==7)

您是否看到了引发另一个异常的选项集?

:System.NotSupportedException
-无效的“where”条件。实体成员正在调用无效的属性或属性method@EkoosstikMartin奇怪的是,我没有这个(或任何)集合内置于上下文对象中。我对Dynamics CRM Online非常陌生,因此可能缺少一个明显的设置。@SteveMallory:生成早期绑定的CRM文件时,您只能访问
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu以及创建
crmorganizationservicecoxt
的早期绑定派生(通常称为
XrmServiceContext
)。您可以在早期绑定文件中看到可用的构造函数。@EkoostikMartin:出于好奇,这是否真的适用于您?我总是得到Steve在第一次评论中得到的相同信息。@PeterMajeed好吧,这个小实用程序非常方便!这是关键。我现在可以使用linq谓词而不必跳过任何障碍。I。e、
.Where(c=>c.new\u AccreditationStatus.Value==7)
。我不希望你的答案被浪费掉,因为它可能会在将来帮助其他人。你能补充另一个答案吗(基本上是重复你的评论),然后我接受。如果你启用代理类型,为什么Account.new\u accreditationstatus必须通过字符串键值访问?它没有为它创建属性访问器吗?new\u accreditationstatus是什么类型的字段?@Daryl它没有。它只在属性集合中可用。我没有设置数据库,也没有我只有通过web服务才能访问。@glosrob:
new#u authenticationstatus
根据上面的转换,看起来是类型
OptionSetValue
。@SteveMallory您应该能够使用CrmSdkUtil重新创建早期绑定对象。当您这样做时,它将创建一个新的早期绑定C文件,其中将包含一个帐户。new#accrEditionStatus属性。它唯一使用的是web服务来创建文件。供将来参考:该实用程序随SDK提供。帮助位于
List<Account> items = _context.CreateQuery<Account>()
                              .Where( c => c["new_accreditationstatus"] == "7"
                              .ToList();
var list = _context.AccountSet.Where(c => 
                     c.FormattedValues["new_accreditationstatus"] == "7").ToList();
var actualValues = _context.CreateQuery("stringmap")
    .Where(x => x.GetAttributeValue<string>("attributename") == "new_accreditationstatus")
    .Where(x => x.GetAttributeValue<int>("objecttypecode") == Xrm.Account.EntityTypeCode);

// This (modified) query uses the StringMap values from the previous query in
// a subquery, linking to the int (attributevalue) value that
// new_accreditationstatus represents.
List<Account> items = _context.CreateQuery<Account>()
    .Where(c => actualValues
        .Where(x => x.GetAttributeValue<int>("attributevalue") == c.new_accreditationstatus.Value)
        .Select(x => x.GetAttributeValue<string>("attributevalue"))
        .Single() == "7")
    .ToList();
var actualValue = _context.CreateQuery("stringmap")
    .Where(x => x.GetAttributeValue<string>("attributename") == "new_accreditationstatus")
    .Where(x => x.GetAttributeValue<int>("value") == "7")
    .Where(x => x.GetAttributeValue<int>("objecttypecode") == Account.EntityTypeCode)
    .Select(x => x.GetAttributeValue<string>("attributevalue"))
    .Single();

List<Account> items = _context.CreateQuery<Account>()
    .Where(c => c.new_accreditationstatus = new OptionSetValue(actualValue)
    .ToList();