LinqToCRM 无法正确转换

发布于 2024-10-23 23:45:43 字数 1529 浏览 1 评论 0原文

我想将所有查询从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-10-30 23:45:43

我在使用 LINQ-to-CRM 时遇到了类似的问题,并返回到 QueryExpressions,因为它们有效,直到我在寻找其他内容时在一些 SDK 示例中找到了解决方案:您需要将 ProxyTypesBehavior 添加到您的IOrganizationService 对象。我不知道它的作用,但这绝对是允许我将 LINQ 与早期绑定类一起使用的更改(据我所知,LINQ-to-CRM 只能与早期绑定课程)。

因此,创建 IOrganizationService 后您需要的行是:

organizationService.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

我希望有所帮助。

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 your IOrganizationService 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:

organizationService.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

I hope that helps.

你的他你的她 2024-10-30 23:45:43

它返回一个 Entity 对象,因此如果您想要一个 System User 对象,您需要调用 ToEntity()。以下内容应该适合您:

var context = new OrganizationServiceContext(crmService);
    SystemUser x = (from c in context.CreateQuery<SystemUser>()
            where (string)c["DomainName"] == @"pfgc\" + Environment.UserName
            select c).FirstOrDefault().ToEntity<SystemUser>();

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:

var context = new OrganizationServiceContext(crmService);
    SystemUser x = (from c in context.CreateQuery<SystemUser>()
            where (string)c["DomainName"] == @"pfgc\" + Environment.UserName
            select c).FirstOrDefault().ToEntity<SystemUser>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文