LinqToCRM 无法正确转换
我想将所有查询从 QueryExpression 更改为 Linq。在开发时,一切似乎都很好,但我总是在运行时遇到强制转换异常(无法将 Microsoft.xrm.sdk.entity 强制转换为 Xrm.SystemUser -> Xrm 是使用 CrmSvcUtil 生成的早期绑定类)。
var context = new OrganizationServiceContext(crmService);
SystemUser x = (from c in context.CreateQuery<SystemUser>()
where c.DomainName == @"pfgc\" + Environment.UserName
select c).FirstOrDefault();
这段代码很简单。我什至尝试过不使用Where 子句,但它不会改变任何东西。
我尝试了以下操作(没有 FirstOrDefault 和 var 而不是 SystemUser)
var x = (from c in context.CreateQuery<SystemUser>()
where c.DomainName == @"pfgc\" + Environment.UserName
select c);
这不会引发异常,但 x 类型是 Microsoft.xrm.sdk.linq.Query。我做错了什么?这似乎正是 SDK 建议要做的事情。
编辑:
GCATNM 有正确的答案。如果有人遇到同样的问题,这里是工作代码的示例:
public SystemUser GetCurrentUser()
{
var context = GetOrgContext();
return (from c in context.CreateQuery<SystemUser>()
where c.DomainName == @"pfgc\" + Environment.UserName
select c).FirstOrDefault();
}
public OrganizationServiceContext GetOrgContext()
{
var serviceProxy1 = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
serviceProxy1.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
return new OrganizationServiceContext(serviceProxy1);
}
I want to change all my queries from QueryExpression to Linq. In development time, all seems to be just fine, but I always get a cast exception at runtime (can't cast Microsoft.xrm.sdk.entity to Xrm.SystemUser -> Xrm is the early bound classes generated with CrmSvcUtil).
var context = new OrganizationServiceContext(crmService);
SystemUser x = (from c in context.CreateQuery<SystemUser>()
where c.DomainName == @"pfgc\" + Environment.UserName
select c).FirstOrDefault();
This code is straightforward. I've even tried without the Where clause and it won't change anything.
I tried the following (no FirstOrDefault and var instead of SystemUser)
var x = (from c in context.CreateQuery<SystemUser>()
where c.DomainName == @"pfgc\" + Environment.UserName
select c);
This won't throw an exception but x type is Microsoft.xrm.sdk.linq.Query. What am I doing wrong? It seems to be exactly what the SDK suggests to do.
EDIT:
GCATNM has the right answer. In case someone faces the same issue, here's a sample of the working code:
public SystemUser GetCurrentUser()
{
var context = GetOrgContext();
return (from c in context.CreateQuery<SystemUser>()
where c.DomainName == @"pfgc\" + Environment.UserName
select c).FirstOrDefault();
}
public OrganizationServiceContext GetOrgContext()
{
var serviceProxy1 = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
serviceProxy1.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
return new OrganizationServiceContext(serviceProxy1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在使用 LINQ-to-CRM 时遇到了类似的问题,并返回到 QueryExpressions,因为它们有效,直到我在寻找其他内容时在一些 SDK 示例中找到了解决方案:您需要将 ProxyTypesBehavior 添加到您的
IOrganizationService
对象。我不知道它的作用,但这绝对是允许我将 LINQ 与早期绑定类一起使用的更改(据我所知,LINQ-to-CRM 只能与早期绑定课程)。因此,创建
IOrganizationService
后您需要的行是:我希望有所帮助。
I had a similar problem with LINQ-to-CRM and went back to QueryExpressions because they worked, until I found the solution in some SDK sample while looking for something else: You need to add a
ProxyTypesBehavior
to yourIOrganizationService
object. I don't know what it does, but that definitely was the change that allowed me to use LINQ with the early bound classes (as I perceive it, LINQ-to-CRM can only be used with the early bound classes).So the line you need after creating your
IOrganizationService
is:I hope that helps.
它返回一个 Entity 对象,因此如果您想要一个 System User 对象,您需要调用 ToEntity()。以下内容应该适合您:
It returns an Entity object, so you'll need to call ToEntity() if you want a System User object. The following should work for you: