2
votes

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!

4
var allaccounts = context.AccountSet.All();lordkain
I did try the All() method but if I don't include any parameters I get "No overload for method All takes 0 parameters". And using snippet => true I get "foreach statement cannot operate on variables of type bool becaue bool does not contain definition for GetEnumerator". I tried ToList() and that seems to work..user2573690

4 Answers

3
votes

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
votes

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.

1
votes

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;
1
votes

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());           
        }