Retrieve list of all accounts in CRM through C#?

[亡魂溺海] 提交于 2019-12-02 02:21:35

问题


I'm trying to retrieve all Account records from CRM 2011 so that I can cycle through them using a ForEach loop and populate a drop down. I was reading this post (Retrieving list of Entities) and am able to retrieve all accounts which meet a certain condition, but how can I retrieve all? That is every single Account record, no matter of the condition?

This is the code I was working with but I don't know which method to use after context.AccountSet. to get all accounts.

var context = new XrmServiceContext();
var parentAccount = context.AccountSet.All(snippet => snippet.ParentAccountId == "Account1");

Using context.AccountSet.All I can get all records which meet the condition, but I don't really need the condition...

Thanks for any help!


回答1:


Why not just retrieve what is pertinent to the drop down?

There are many attributes that Account has that will just bloat the query.

/* If you only want name */
var accounts = context.AccountSet.Select(acc => acc.Name);
/* If you want more attributes */
var accounts = context.AccountSet
    .Select(acc => new
        {
            name = acc.Name,
            guid = acc.AccountId,
            parent = acc.ParentAccountId,
            number = acc.AccountNumber
        });
/* No need to call .ToList() on accounts, just iterate through the IQuerable */
foreach (var account in accounts)
{
    // Add account to drop down
}



回答2:


AccountSet already contains all the records, this is the reason why if you do a .ToList() you get a List of Account, because you convert the AccontSet collection to a List.




回答3:


try this:

var parentAccount =  (from c in context.CreateQuery<Account>()
                     select c);

If it's not returning the right type instead of var use

IEnumerable<Account>

Also you may need to include

using System.Linq;



回答4:


Firstly you should have a connection to the CRM .Either by CRMConnection Class or by latest CRM 2016 connection method. link below. https://msdn.microsoft.com/en-in/library/jj602970.aspx

After establishing connection. You can use QueryExpression Class to query the entity and Store the entire data in a ColumnSet which is a set of columns as name suggests. This code store account names and id in crm to individual list. as simple as that kindly note the columnset takes a string parameter so the string array of columnname can be passed to it. Thanks and let me know if you have a doubt.

 QueryExpression query = new QueryExpression("account");
        query.ColumnSet.AddColumns("name","accountid");
    //  query.Criteria.AddFilter(filter1);

        EntityCollection result1 = service.RetrieveMultiple(query);
        Console.WriteLine(); Console.WriteLine("Query using Query Expression with ConditionExpression and FilterExpression");
        Console.WriteLine("---------------------------------------");

        foreach (var a in result1.Entities)
        {
            //Console.WriteLine("Name: " + a.Attributes["name"]);
            ListAccountName.Add(a.Attributes["name"].ToString());
            ListAccountId.Add(a.Attributes["accountid"].ToString());           
        }           


来源:https://stackoverflow.com/questions/20082365/retrieve-list-of-all-accounts-in-crm-through-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!