使用LINQ从Microsoft CRM检索数据时出现奇怪的行为 [英] Getting weird behavior when retrieving data from Microsoft CRM using LINQ

查看:52
本文介绍了使用LINQ从Microsoft CRM检索数据时出现奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用LINQ访问Contact实体时遇到问题.

I'm having this problem accessing the Contact entity using LINQ.

我有以下两个功能.

如果我运行第一个函数然后调用第二个函数,那么我似乎在第二个查询中缺少很多字段.像名字和姓氏都没有显示.它们只是显示为空值.如果我自己运行第二个函数,那么我将获得正确的数据.在这两次运行中,正确显示的唯一字段是Id,ContactId和new_username.

If I ran the 1st function and then call the 2nd one, I seemed to be missing a lot of fields in the 2nd query. Like firstname and lastname are not showing up. They just shows up as null values. If I ran the 2nd function on its own, I am getting the right data. The only fields that shows up correctly in both runs are Id, ContactId and new_username.

如果我自己运行第二个函数,那么我将获得正确的数据.

If I ran the 2nd function on its own, I am getting the right data.

有什么主意我在做错什么吗?

Any ideas what am I doing wrong?

非常感谢

这是2个功能

    public List<String> GetContactsUsernameOnly()
    {
        IQueryable<String> _records = from _contactSet in _flinsafeContext.ContactSet
                                       where
                                           _contactSet.new_FAN == "username"
                                       orderby _contactSet.new_username
                                       select _contactSet.new_username;

        return _records.ToList();
    }

    public List<Contact> GetContacts()
    {
        IQueryable<Contact> _records = from _contactSet in _flinsafeContext.ContactSet
                                       where
                                           _contactSet.new_FAN == "my-username-here"
                                       orderby _contactSet.new_username
                                       select _contactSet;

        return _records.ToList();
    }

推荐答案

这是因为您在调用这两种方法(在您的情况下为_flinsafeContext)时都在重用相同的CRM上下文

It is because you are reusing the same CRM context when you call both methods (in your case _flinsafeContext)

上下文所做的是缓存记录,因此第一种方法是返回您的联系人,但只返回new_username字段.

What the context does is cache records, so the first method is returning your contact but only bringing back the new_username field.

第二种方法想要返回整个记录,但是当在第一个方法之后调用它时,该记录已经存在于上下文中,因此尽管只填充了一个字段,但它只返回该记录.延迟加载尚未填充的字段还不够聪明.如果首先调用此方法,则该方法在上下文中不存在,因此将返回整个记录.

The second method wants to return the whole record, but when it is called after the first one the record already exists in the context so it just returns that, despite only having the one field populated. It is not clever enough to lazy load the fields that have not been populated. If this method was called first, it doesn't exist in the context so will return the whole record.

有两种方法可以解决此问题:

There are 2 ways to get around this:

1)不要重用CRMContext.而是在基于单例IOrganizationService的每种方法中创建一个新方法.

1) Don't reuse CRMContexts. Instead create a new one in each method based on a singleton IOrganizationService.

2)您的上下文中有一个ClearChanges()方法,这意味着下次您执行查询时,它将返回到CRM并获取您选择的字段.这还将清除所有未保存的创建/更新/删除"等内容,因此您必须注意上下文所处的状态.

2) There is a ClearChanges() method on your context that will mean the next time you do a query it will go back to CRM and get the fields you have selected. This will also clear any unsaved Created/Updates/Deletes etc so you have to be careful around what state the context is in.

顺便说一句,创建新的CRM上下文并不是一项繁重的操作,因此通常不值得传递上下文并重新使用它们.它正在创建最慢的底层OrganisationService.

As an aside, creating a new CRM Context isn't an intensive operation so it's not often worthwhile passing contexts around and reusing them. It is creating the underlying OrganisationService that is the slowest bit.

此行为可能会非常痛苦,因为返回整个记录的效率非常低而且很慢,因此您只想为每个查询选择想要的字段.

This behaviour can be so painful, because it is horribly inefficient and slow to return the entire record so you WANT to be selecting only the fields you want for each query.

这篇关于使用LINQ从Microsoft CRM检索数据时出现奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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