Sample: Complex LINQ queries
Applies To: Dynamics CRM 2013
This sample code is for Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online. Download the Microsoft Dynamics CRM SDK package. The sample can be found in the following location in the download package:
SampleCode\CS\GeneralProgramming\Queries\LINQExamples.cs
SampleCode\VB\GeneralProgramming\Queries\LINQExamples.vb
Requirements
For more information about the requirements for running the sample code provided in this SDK, see Use the sample and helper code.
Demonstrates
This sample shows how to use .NET Language-Integrated Query (LINQ) with Microsoft Dynamics CRM 2013 and Microsoft Dynamics CRM Online.
Example
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
// These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
// located in the SDK\bin folder of the SDK download.
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Messages;
// This namespace is found in Microsoft.Crm.Sdk.Proxy.dll assembly
// found in the SDK\bin folder.
using Microsoft.Crm.Sdk.Messages;
using System.Linq; //namespace for LINQ queries
namespace Microsoft.Crm.Sdk.Samples
{
/// <summary>
/// Demonstrates basic and complex LINQ queries used with Microsoft Dynamics CRM
/// </summary>
/// <remarks>
/// At run-time, you will be given the option to delete all the
/// database records created by this program.</remarks>
public class LINQ101
{
#region Class Level Members
private Guid _contactId1;
private Guid _contactId2;
private Guid _contactId3;
private Guid _contactId4;
private Guid _accountId1;
private Guid _accountId2;
private Guid _incidentId1;
private Guid _incidentId2;
private Guid _leadId;
private OrganizationServiceProxy _serviceProxy;
#endregion Class Level Members
#region How To Sample Code
/// <summary>
/// This method first connects to the Organization service. Afterwards,
/// a series of LINQ queries are performed.
/// </summary>
/// <param name="serverConfig">Contains server connection information.</param>
/// <param name="promptforDelete">When True, the user will be prompted to delete all
/// created entities.</param>
public void Run(ServerConnection.Configuration serverConfig, bool promptforDelete)
{
try
{
// Connect to the Organization service.
// The using statement assures that the service proxy will be properly disposed.
using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
{
// This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes();
CreateRequiredRecords();
#region EarlyBoundExamples
// *****************************************************************************************************************
// LNQ Simple where clause 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Accounts using one where clause");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_where1 = from a in svcContext.AccountSet
where a.Name.Contains("Contoso")
select a;
foreach (var a in query_where1)
{
System.Console.WriteLine(a.Name + " " + a.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Accounts using where clause 1
//======================================
//Contoso Ltd Redmond
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Simple where clause 2
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Accounts using two where clauses");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_where2 = from a in svcContext.AccountSet
where a.Name.Contains("Contoso")
where a.Address1_City == "Redmond"
select a;
foreach (var a in query_where2)
{
System.Console.WriteLine(a.Name + " " + a.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Accounts using where clause 2
//======================================
//Contoso Ltd Redmond
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Join and simple where clause query
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Account and Contact Info using where clause");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_where3 = from c in svcContext.ContactSet
join a in svcContext.AccountSet
on c.ContactId equals a.PrimaryContactId.Id
where a.Name.Contains("Contoso")
where c.LastName.Contains("Smith")
select new
{
account_name = a.Name,
contact_name = c.LastName
};
foreach (var c in query_where3)
{
System.Console.WriteLine("acct: " +
c.account_name +
"\t\t\t" +
"contact: " +
c.contact_name);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Account and Contact Info using where clause
//======================================
//acct: Contoso Ltd contact: Smith
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the Distinct operator (returns only one of duplicate elements)
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using Distinct operator");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_distinct = (from c in svcContext.ContactSet
select c.LastName).Distinct();
foreach (var c in query_distinct)
{
System.Console.WriteLine(c);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using Distinct operator
//======================================
//Parker
//Smith
//Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Simple inner join 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact and Account Info Using join 1 ");
System.Console.WriteLine("==================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join1 = from c in svcContext.ContactSet
join a in svcContext.AccountSet
on c.ContactId equals a.PrimaryContactId.Id
select new
{
c.FullName,
c.Address1_City,
a.Name,
a.Address1_Name
};
foreach (var c in query_join1)
{
System.Console.WriteLine("acct: " +
c.Name +
"\t\t\t" +
"contact: " +
c.FullName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact and Account Info Using join 1
//==================================
//acct: Contoso Ltd contact: Brian Smith
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Multiple join 4
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact, Account, Lead Info using multiple join 4");
System.Console.WriteLine("==================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join4 = from a in svcContext.AccountSet
join c in svcContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
join l in svcContext.LeadSet
on a.OriginatingLeadId.Id equals l.LeadId
select new
{
contact_name = c.FullName,
account_name = a.Name,
lead_name = l.FullName
};
foreach (var c in query_join4)
{
System.Console.WriteLine(c.contact_name +
" " +
c.account_name +
" " +
c.lead_name);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
//List of Contact, Account, Lead Info using multiple join 4
//==================================
//Brian Smith Contoso Ltd Diogo Andrade
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Self join 5
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Account Info using self join 5");
System.Console.WriteLine("==================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join5 = from a in svcContext.AccountSet
join a2 in svcContext.AccountSet
on a.ParentAccountId.Id equals a2.AccountId
select new
{
account_name = a.Name,
account_city = a.Address1_City
};
foreach (var c in query_join5)
{
System.Console.WriteLine(c.account_name + " " + c.account_city);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
//List of Account Info using self join 5
//==================================
//Contoso Ltd Redmond
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Double join 6
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using double join 6");
System.Console.WriteLine("==================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join6 = from c in svcContext.ContactSet
join a in svcContext.AccountSet
on c.ContactId equals a.PrimaryContactId.Id
join a2 in svcContext.AccountSet
on a.ParentAccountId.Id equals a2.AccountId
select new
{
contact_name = c.FullName,
account_name = a.Name
};
foreach (var c in query_join6)
{
System.Console.WriteLine(c.contact_name + " " + c.account_name);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
//List of Contact Info using double join 6
//==================================
//Brian Smith Contoso Ltd
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Entity Fields join 7
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using entity field join 7");
System.Console.WriteLine("==================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var list_join = (from a in svcContext.AccountSet
join c in svcContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
where a.Name == "Contoso Ltd" &&
a.Address1_Name == "Contoso Pharmaceuticals"
select a).ToList();
foreach (var c in list_join)
{
System.Console.WriteLine("Account " + list_join[0].Name
+ " and it's primary contact "
+ list_join[0].PrimaryContactId.Id);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
// Account Contoso Ltd and it's primary contact 918228af-04a1-e011-b1b7-00155dba3818
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Left join 8
// A left join is designed to return parents with and without children from two sources.
// There is a correlation between parent and child, but no child may actually exist.
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using left join 8");
System.Console.WriteLine("==================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_join8 = from a in svcContext.AccountSet
join c in svcContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
into gr
from c_joined in gr.DefaultIfEmpty()
select new
{
contact_name = c_joined.FullName,
account_name = a.Name
};
foreach (var c in query_join8)
{
System.Console.WriteLine(c.contact_name + " " + c.account_name);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
//List of Contact Info using left join 8
//==================================
// Coho Winery
//Brian Smith Contoso Ltd
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the Equals operator 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using Equals operator 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_equals1 = from c in svcContext.ContactSet
where c.FirstName.Equals("Colin")
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_equals1)
{
System.Console.WriteLine(c.FirstName +
" " + c.LastName +
" " + c.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using Equals operator 1
//======================================
//Colin Wilcox Redmond
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the Equals operator 2
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using Equals operator 2");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_equals2 = from c in svcContext.ContactSet
where c.FamilyStatusCode.Equals(3)
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_equals2)
{
System.Console.WriteLine(c.FirstName +
" " + c.LastName +
" " + c.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using Equals operator 2
//======================================
//Brian Smith Bellevue
//Darren Parker Kirkland
//Ben Smith Kirkland
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the not equals operator 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using not equals operator 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_ne1 = from c in svcContext.ContactSet
where c.Address1_City != "Redmond"
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_ne1)
{
System.Console.WriteLine(c.FirstName + " " +
c.LastName + " " + c.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using not equals operator 1
//======================================
//Brian Smith Bellevue
//Darren Parker Kirkland
//Ben Smith Kirkland
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the not equals operator 2
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using not equals operator 2");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_ne2 = from c in svcContext.ContactSet
where !c.FirstName.Equals("Colin")
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_ne2)
{
System.Console.WriteLine(c.FirstName + " " +
c.LastName + " " + c.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using not equals operator 2
//======================================
//Brian Smith Bellevue
//Darren Parker Kirkland
//Ben Smith Kirkland
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LINQ Method-based LINQ query with where clause
// *****************************************************************************************************************
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var methodResults = svcContext.ContactSet
.Where(a => a.LastName == "Smith");
var methodResults2 = svcContext.ContactSet
.Where(a => a.LastName.StartsWith("Smi"));
Console.WriteLine();
Console.WriteLine("Method query using Lambda expression");
Console.WriteLine("---------------------------------------");
foreach (var a in methodResults)
{
Console.WriteLine("Name: " + a.FirstName + " " + a.LastName);
}
Console.WriteLine("---------------------------------------");
Console.WriteLine("Method query 2 using Lambda expression");
Console.WriteLine("---------------------------------------");
foreach (var a in methodResults2)
{
Console.WriteLine("Name: " + a.Attributes["firstname"] +
" " + a.Attributes["lastname"]);
}
}
Console.WriteLine("---------------------------------------");
// OUTPUT:
//Method query using Lambda expression
//---------------------------------------
//Name: Brian Smith
//Name: Ben Smith
//---------------------------------------
//Method query 2 using Lambda expression
//---------------------------------------
//Name: Brian Smith
//Name: Ben Smith
//---------------------------------------
// *****************************************************************************************************************
// LNQ Using the greater than operator 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using greater than operator 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_gt1 = from c in svcContext.ContactSet
where c.Anniversary > new DateTime(2010, 2, 5)
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_gt1)
{
System.Console.WriteLine(c.FirstName + " " +
c.LastName + " " + c.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using greater than operator 1
//======================================
//Colin Wilcox Redmond
//Brian Smith Bellevue
//Darren Parker Kirkland
//Ben Smith Kirkland
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the greater than operator 2
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using greater than operator 2");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_gt2 = from c in svcContext.ContactSet
where c.CreditLimit.Value > 20000
select new
{
c.FirstName,
c.LastName,
c.Address1_City
};
foreach (var c in query_gt2)
{
System.Console.WriteLine(c.FirstName + " " +
c.LastName + " " + c.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using greater than operator 2
//======================================
//Brian Smith Bellevue
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the ge and le operators 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the ge and le operators 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_gele1 = from c in svcContext.ContactSet
where c.CreditLimit.Value >= 200 &&
c.CreditLimit.Value <= 400
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_gele1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using the ge and le operators 1
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the contains operator 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the contains operator 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_contains1 = from c in svcContext.ContactSet
where c.Description.Contains("Alpine")
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_contains1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the contains operator 1
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the negated contains operator 2
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the negated contains operator 2");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_contains2 = from c in svcContext.ContactSet
where !c.Description.Contains("Coho")
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_contains2)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the negated contains operator 2
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the StartsWith operator 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the StartsWith operator 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_startswith1 = from c in svcContext.ContactSet
where c.FirstName.StartsWith("Bri")
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_startswith1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the StartsWith operator 1
//======================================
//Brian Smith
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the endswith operator 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the EndsWith operator 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_endswith1 = from c in svcContext.ContactSet
where c.LastName.EndsWith("cox")
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_endswith1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the EndsWith operator 1
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the && and || operators 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the && and || operators 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_andor1 = from c in svcContext.ContactSet
where ((c.Address1_City == "Redmond" ||
c.Address1_City == "Bellevue") &&
(c.CreditLimit.Value != null &&
c.CreditLimit.Value >= 200))
select c;
foreach (var c in query_andor1)
{
System.Console.WriteLine(c.LastName + ", " + c.FirstName + " " +
c.Address1_City + " " + c.CreditLimit.Value);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using the && and || operators 1
//======================================
//Wilcox, Colin Redmond 300.0000
//Smith, Brian Bellevue 30000.0000
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the orderby operator 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the orderby operator 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_orderby1 = from c in svcContext.ContactSet
where !c.CreditLimit.Equals(null)
orderby c.CreditLimit descending
select new
{
limit = c.CreditLimit,
first = c.FirstName,
last = c.LastName
};
foreach (var c in query_orderby1)
{
System.Console.WriteLine(c.limit.Value + " " +
c.last + ", " + c.first);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the orderby operator 1
//======================================
//30000.0000 Smith, Brian
//12000.0000 Smith, Ben
//10000.0000 Parker, Darren
//300.0000 Wilcox, Colin
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the orderby operator 2
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the orderby operator 2");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_orderby2 = from c in svcContext.ContactSet
orderby c.LastName descending,
c.FirstName ascending
select new
{
first = c.FirstName,
last = c.LastName
};
foreach (var c in query_orderby2)
{
System.Console.WriteLine(c.last + ", " + c.first);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the orderby operator 2
//======================================
//Wilcox, Colin
//Smith, Ben
//Smith, Brian
//Parker, Darren
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the First and Single operators
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("==========================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
Contact firstcontact = svcContext.ContactSet.First();
Contact singlecontact = svcContext.ContactSet.Single(c => c.ContactId == _contactId1);
System.Console.WriteLine(firstcontact.LastName + ", " +
firstcontact.FirstName + " is the first contact");
System.Console.WriteLine("==========================");
System.Console.WriteLine(singlecontact.LastName + ", " +
singlecontact.FirstName + " is the single contact");
}
System.Console.WriteLine("======================================");
// OUTPUT:
//==========================
//Wilcox, Colin is the first contact
//==========================
//Wilcox, Colin is the single contact
//======================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Retrieving formatted values 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("Retrieving formatted values 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var list_retrieve1 = from c in svcContext.ContactSet
where c.ContactId == _contactId1
select new { StatusReason = c.FormattedValues["statuscode"] };
foreach (var c in list_retrieve1)
{
System.Console.WriteLine("Status: " + c.StatusReason);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//Retrieving formatted values 1
//======================================
//Status: Active
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the Skip and Take operators (non-paging)
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the Skip and Take operators");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_skip = (from c in svcContext.ContactSet
where c.LastName != "Parker"
orderby c.FirstName
select new
{
last = c.LastName,
first = c.FirstName
}).Skip(2).Take(2);
foreach (var c in query_skip)
{
System.Console.WriteLine(c.first + " " + c.last);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the Skip and Take operators
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the FirstOrDefault and SingleOrDefault operators
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("==========================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
Contact firstorcontact = svcContext.ContactSet.FirstOrDefault();
Contact singleorcontact = svcContext.ContactSet
.SingleOrDefault(c => c.ContactId == _contactId1);
System.Console.WriteLine(firstorcontact.FullName +
" is the first contact");
System.Console.WriteLine("==========================");
System.Console.WriteLine(singleorcontact.FullName +
" is the single contact");
}
System.Console.WriteLine("======================================");
// OUTPUT:
//==========================
//Colin Wilcox is the first contact
//==========================
//Colin Wilcox is the single contact
//======================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a self join with a condition on the linked entity
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using a self join with a condition on the linked entity");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_joincond = from a1 in svcContext.AccountSet
join a2 in svcContext.AccountSet
on a1.ParentAccountId.Id equals a2.AccountId
where a2.AccountId == _accountId1
select new { Account = a1, Parent = a2 };
foreach (var a in query_joincond)
{
System.Console.WriteLine(a.Account.Name + " " + a.Parent.Name);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using a self join with a condition on the linked entity
//======================================
//Contoso Ltd Coho Winery
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a transformation in the where clause
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using a transformation in the where clause");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_wheretrans = from c in svcContext.ContactSet
where c.ContactId == _contactId1 &&
c.Anniversary > DateTime.Parse("1/1/2010")
select new
{
c.FirstName,
c.LastName
};
foreach (var c in query_wheretrans)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using a transformation in the where clause
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a lookup value to order by
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Account Info using a lookup value to order by");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_orderbylookup = from a in svcContext.AccountSet
where a.Address1_Name == "Contoso Pharmaceuticals"
orderby a.PrimaryContactId
select new
{
a.Name,
a.Address1_City
};
foreach (var a in query_orderbylookup)
{
System.Console.WriteLine(a.Name + " " + a.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Account Info using a lookup value to order by
//======================================
//Contoso Ltd Redmond
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a a picklist to order by
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using a picklist to order by");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_orderbypicklist = from c in svcContext.ContactSet
where c.LastName != "Parker" &&
c.AccountRoleCode != null
orderby c.AccountRoleCode, c.FirstName
select new
{
AccountRole = c.FormattedValues["accountrolecode"],
c.FirstName,
c.LastName
};
foreach (var c in query_orderbypicklist)
{
System.Console.WriteLine(c.AccountRole + " " +
c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using a picklist to order by
//======================================
//Decision Maker Colin Wilcox
//Employee Ben Smith
//Employee Brian Smith
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a paging sort 1
// (Multi-column sort with extra condition)
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using a paging sort 1");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_pagingsort1 = (from c in svcContext.ContactSet
where c.LastName != "Parker"
orderby c.LastName ascending,
c.FirstName descending
select new { c.FirstName, c.LastName })
.Skip(2).Take(2);
foreach (var c in query_pagingsort1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using a paging sort 1
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a paging sort 2
// (Page and sort where the column being sorted is different from the column being retrieved
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using a paging sort 2");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_pagingsort2 = (from c in svcContext.ContactSet
where c.LastName != "Parker"
orderby c.FirstName descending
select new { c.FirstName }).Skip(2).Take(2);
foreach (var c in query_pagingsort2)
{
System.Console.WriteLine(c.FirstName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using a paging sort 2
//======================================
//Ben
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a paging sort 3
// (Creates only the first page)
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using a paging sort 3");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_pagingsort3 = (from c in svcContext.ContactSet
where c.LastName.StartsWith("W")
orderby c.MiddleName ascending,
c.FirstName descending
select new
{
c.FirstName,
c.MiddleName,
c.LastName
}).Take(10);
foreach (var c in query_pagingsort3)
{
System.Console.WriteLine(c.FirstName + " " +
c.MiddleName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using a paging sort 3
//======================================
//Colin Wilcox
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Retrieving related entity columns (for 1 to N relationships)
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact and Account Info by retrieving related entity columns");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_retrieve1 = from c in svcContext.ContactSet
join a in svcContext.AccountSet
on c.ContactId equals a.PrimaryContactId.Id
where c.ContactId != _contactId1
select new { Contact = c, Account = a };
foreach (var c in query_retrieve1)
{
System.Console.WriteLine("Acct: " + c.Account.Name +
"\t\t" + "Contact: " + c.Contact.FullName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact and Account Info by retrieving related entity columns
//======================================
//Acct: Contoso Ltd Contact: Brian Smith
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using .Value to retrieve the value of an attribute
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using .Value to retrieve the value of an attribute");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_value = from c in svcContext.ContactSet
where c.ContactId != _contactId2
select new
{
ContactId = c.ContactId != null ?
c.ContactId.Value : Guid.Empty,
NumberOfChildren = c.NumberOfChildren != null ?
c.NumberOfChildren.Value : default(int),
CreditOnHold = c.CreditOnHold != null ?
c.CreditOnHold.Value : default(bool),
Anniversary = c.Anniversary != null ?
c.Anniversary.Value : default(DateTime)
};
foreach (var c in query_value)
{
System.Console.WriteLine(c.ContactId + " " + c.NumberOfChildren +
" " + c.CreditOnHold + " " + c.Anniversary);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using .Value to retrieve the value of an attribute
//======================================
//8f8228af-04a1-e011-b1b7-00155dba3818 1 False 3/5/2010 8:00:00 AM
//938228af-04a1-e011-b1b7-00155dba3818 2 False 10/5/2010 7:00:00 AM
//958228af-04a1-e011-b1b7-00155dba3818 2 True 7/5/2010 7:00:00 AM
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Multiple projections, new data type casting to different types
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using multiple projections");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_projections = from c in svcContext.ContactSet
where c.ContactId == _contactId1
&& c.NumberOfChildren != null &&
c.Anniversary.Value != null
select new
{
Contact = new Contact {
LastName = c.LastName,
NumberOfChildren = c.NumberOfChildren
},
NumberOfChildren = (double)c.NumberOfChildren,
Anniversary = c.Anniversary.Value.AddYears(1),
};
foreach (var c in query_projections)
{
System.Console.WriteLine(c.Contact.LastName + " " +
c.NumberOfChildren + " " + c.Anniversary);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using multiple projections
//======================================
//Wilcox 1 3/5/2011 8:00:00 AM
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the GetAttributeValue method
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using the GetAttributeValue method");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_getattrib = from c in svcContext.ContactSet
where c.GetAttributeValue<Guid>("contactid") != _contactId1
select new
{
ContactId = c.GetAttributeValue<Guid?>("contactid"),
NumberOfChildren = c.GetAttributeValue<int?>("numberofchildren"),
CreditOnHold = c.GetAttributeValue<bool?>("creditonhold"),
Anniversary = c.GetAttributeValue<DateTime?>("anniversary"),
};
foreach (var c in query_getattrib)
{
System.Console.WriteLine(c.ContactId + " " + c.NumberOfChildren +
" " + c.CreditOnHold + " " + c.Anniversary);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using the GetAttributeValue method
//======================================
//918228af-04a1-e011-b1b7-00155dba3818 2 False 4/5/2010 7:00:00 AM
//938228af-04a1-e011-b1b7-00155dba3818 2 False 10/5/2010 7:00:00 AM
//958228af-04a1-e011-b1b7-00155dba3818 2 True 7/5/2010 7:00:00 AM
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using math operations
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using math operations");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_math = from c in svcContext.ContactSet
where c.ContactId != _contactId2
&& c.Address1_Latitude != null &&
c.Address1_Longitude != null
select new
{
Round = Math.Round(c.Address1_Latitude.Value),
Floor = Math.Floor(c.Address1_Latitude.Value),
Ceiling = Math.Ceiling(c.Address1_Latitude.Value),
Abs = Math.Abs(c.Address1_Latitude.Value),
};
foreach (var c in query_math)
{
System.Console.WriteLine(c.Round + " " + c.Floor +
" " + c.Ceiling + " " + c.Abs);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using math operations
//======================================
//48 47 48 47.67417
//48 47 48 47.61056
//48 47 48 47.61056
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using multiple select and where clauses
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Incidents using multiple select and where clauses");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_multiselect = svcContext.IncidentSet
.Where(i => i.IncidentId != _incidentId1)
.Select(i => i.incident_customer_accounts)
.Where(a => a.AccountId != _accountId2)
.Select(a => a.account_primary_contact)
.OrderBy(c => c.FirstName)
.Select(c => c.ContactId);
foreach (var c in query_multiselect)
{
System.Console.WriteLine(c.GetValueOrDefault());
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Incidents using multiple select and where clauses
//======================================
//918228af-04a1-e011-b1b7-00155dba3818
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using SelectMany
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Account Info using SelectMany");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_selectmany = svcContext.ContactSet
.Where(c => c.ContactId != _contactId2)
.SelectMany(c => c.account_primary_contact)
.OrderBy(a => a.Name);
foreach (var c in query_selectmany)
{
System.Console.WriteLine(c.AccountId + " " + c.Name);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Account Info using SelectMany
//======================================
//9b8228af-04a1-e011-b1b7-00155dba3818 Contoso Ltd
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using String operations
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using String operations");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_string = from c in svcContext.ContactSet
where c.ContactId == _contactId2
select new
{
IndexOf = c.FirstName.IndexOf("contact"),
Insert = c.FirstName.Insert(1, "Insert"),
Remove = c.FirstName.Remove(1, 1),
Substring = c.FirstName.Substring(1, 1),
ToUpper = c.FirstName.ToUpper(),
ToLower = c.FirstName.ToLower(),
TrimStart = c.FirstName.TrimStart(),
TrimEnd = c.FirstName.TrimEnd(),
};
foreach (var c in query_string)
{
System.Console.WriteLine(c.IndexOf + "\n" + c.Insert + "\n" +
c.Remove + "\n" + c.Substring + "\n"
+ c.ToUpper + "\n" + c.ToLower +
"\n" + c.TrimStart + " " + c.TrimEnd);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using String operations
//======================================
//-1
//BInsertrian
//Bian
//r
//BRIAN
//brian
//Brian Brian
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using Two Where Clauses
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Account and Contact Info using two where clauses");
System.Console.WriteLine("======================================");
using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
{
var query_twowhere = from a in svcContext.AccountSet
join c in svcContext.ContactSet
on a.PrimaryContactId.Id equals c.ContactId
where c.LastName == "Smith" && c.CreditOnHold != null
where a.Name == "Contoso Ltd"
orderby a.Name
select a;
foreach (var c in query_twowhere)
{
System.Console.WriteLine(c.AccountId + " " + c.Name);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Account and Contact Info using two where clauses
//======================================
//9b8228af-04a1-e011-b1b7-00155dba3818 Contoso Ltd
//=====================================
System.Console.WriteLine();
#endregion EarlyBoundExamples
#region LateBoundExamples
// *****************************************************************************************************************
// LNQ Using late-bound entities, simple inner join 2
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact and Account Info using late-bound entities with join 2");
System.Console.WriteLine("==================================");
using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
var query_join2 = from c in orgSvcContext.CreateQuery("contact")
join a in orgSvcContext.CreateQuery("account")
on c["contactid"] equals a["primarycontactid"]
select new
{
contact_name = c["fullname"],
account_name = a["name"]
};
foreach (var c in query_join2)
{
System.Console.WriteLine(c.contact_name + " " + c.account_name);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
//List of Contact and Account Info using late-bound entities with join 2
//==================================
//Brian Smith Contoso Ltd
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Alternative syntax simple inner join 3
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact and Account Info with alternative syntax join 3");
System.Console.WriteLine("==================================");
using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
var query_join3 = from c in orgSvcContext.CreateQuery("contact")
join a in orgSvcContext.CreateQuery("account")
on c["contactid"] equals (Guid)((EntityReference)a["primarycontactid"]).Id
select new
{
contact_name = c["fullname"],
account_name = a["name"]
};
foreach (var c in query_join3)
{
System.Console.WriteLine(c.contact_name + " " + c.account_name);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
//List of Contact and Account Info with alternative syntax join 3
//==================================
//Brian Smith Contoso Ltd
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Late binding left join 9
// A left join is designed to return parents with and without children from two sources.
// There is a correlation between parent and child, but no child may actually exist.
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using late binding left join 9");
System.Console.WriteLine("==================================");
using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
var query_join9 = from a in orgSvcContext.CreateQuery("account")
join c in orgSvcContext.CreateQuery("contact")
on a["primarycontactid"] equals c["contactid"] into gr
from c_joined in gr.DefaultIfEmpty()
select new
{
account_name = a.Attributes["name"]
};
foreach (var c in query_join9)
{
System.Console.WriteLine(c.account_name);
}
}
System.Console.WriteLine("==================================");
// OUTPUT:
//List of Contact Info using late binding left join 9
//==================================
//Coho Winery
//Contoso Ltd
//==================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using late binding and not equals operator 3
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using late binding and not equals operator 3");
System.Console.WriteLine("======================================");
using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
var query_ne3 = from c in orgSvcContext.CreateQuery("contact")
where !c["address1_city"].Equals(null)
select new
{
FirstName = c["firstname"],
LastName = c["lastname"],
Address1_City = c["address1_city"]
};
foreach (var c in query_ne3)
{
System.Console.WriteLine(c.FirstName + " " +
c.LastName + " " + c.Address1_City);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
// List of Contact Info using late binding and not equals operator 3
//======================================
//Colin Wilcox Redmond
//Brian Smith Bellevue
//Darren Parker Kirkland
//Ben Smith Kirkland
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using late binding with Contains operator 3
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using late binding with Contains operator 3");
System.Console.WriteLine("======================================");
using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
var query_contains3 = from c in orgSvcContext.CreateQuery("contact")
where ((string)c["description"]).Contains("Coho")
select new
{
firstname = c.Attributes["firstname"],
lastname = c.Attributes["lastname"]
};
foreach (var c in query_contains3)
{
System.Console.WriteLine(c.firstname + " " + c.lastname);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using late binding with Contains operator 3
//======================================
//Brian Smith
//Darren Parker
//Ben Smith
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using the GetAttributeValue (late binding) method 1
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Contact Info using late binding and the GetAttributeValue method 2");
System.Console.WriteLine("======================================");
using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
var list_getattrib1 = (from c in orgSvcContext.CreateQuery("contact")
where c.GetAttributeValue<Guid?>("contactid") != _contactId1
select new {
FirstName = c.GetAttributeValue<string>("firstname"),
LastName = c.GetAttributeValue<string>("lastname")
}).ToList();
foreach (var c in list_getattrib1)
{
System.Console.WriteLine(c.FirstName + " " + c.LastName);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Contact Info using late binding and the GetAttributeValue method 2
//======================================
//Brian Smith
//Darren Parker
//Ben Smith
//=====================================
System.Console.WriteLine();
// *****************************************************************************************************************
// LNQ Using a late binding join
// *****************************************************************************************************************
System.Console.WriteLine();
System.Console.WriteLine("List of Info using a late binding join");
System.Console.WriteLine("======================================");
using (OrganizationServiceContext orgSvcContext = new OrganizationServiceContext(_serviceProxy))
{
var query_dejoin = from c in orgSvcContext.CreateQuery("contact")
join a in orgSvcContext.CreateQuery("account")
on c["contactid"] equals a["primarycontactid"]
join l in orgSvcContext.CreateQuery("lead")
on a["originatingleadid"] equals l["leadid"]
where (string)c["lastname"] != "Parker"
select new { Contact = c, Account = a, Lead = l };
foreach (var c in query_dejoin)
{
System.Console.WriteLine(c.Account.Attributes["name"] + " " +
c.Contact.Attributes["fullname"] + " " + c.Lead.Attributes["leadid"]);
}
}
System.Console.WriteLine("=====================================");
// OUTPUT:
//List of Info using a late binding join
//======================================
//Contoso Ltd Brian Smith 9a8228af-04a1-e011-b1b7-00155dba3818
//=====================================
System.Console.WriteLine();
#endregion LateBoundExamples
DeleteRequiredRecords(promptforDelete);
}
}
// Catch any service fault exceptions that Microsoft Dynamics CRM throws.
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
{
// You can handle an exception here or pass it back to the calling method.
throw;
}
}
/// <summary>
/// Creates any entity records that this sample requires.
/// </summary>
public void CreateRequiredRecords()
{
Contact contact1 = new Contact
{
FirstName = "Colin",
LastName = "Wilcox",
Address1_City = "Redmond",
Address1_StateOrProvince = "WA",
Address1_PostalCode = "98052",
Anniversary = new DateTime(2010, 3, 5),
CreditLimit = new Money(300),
Description = "Alpine Ski House",
StatusCode = new OptionSetValue(1),
AccountRoleCode = new OptionSetValue(1),
NumberOfChildren = 1,
Address1_Latitude = 47.6741667,
Address1_Longitude = -122.1202778,
CreditOnHold = false
};
_contactId1 = _serviceProxy.Create(contact1);
Console.Write("Created a sample contact 1: {0}, ", contact1.FirstName + " " + contact1.LastName);
Contact contact2 = new Contact
{
FirstName = "Brian",
LastName = "Smith",
Address1_City = "Bellevue",
FamilyStatusCode = new OptionSetValue(3),
Address1_StateOrProvince = "WA",
Address1_PostalCode = "98008",
Anniversary = new DateTime(2010, 4, 5),
CreditLimit = new Money(30000),
Description = "Coho Winery",
StatusCode = new OptionSetValue(1),
AccountRoleCode = new OptionSetValue(2),
NumberOfChildren = 2,
Address1_Latitude = 47.6105556,
Address1_Longitude = -122.1994444,
CreditOnHold = false
};
_contactId2 = _serviceProxy.Create(contact2);
Console.Write("Created a sample contact 2: {0}, ", contact2.FirstName + " " + contact2.LastName);
Contact contact3 = new Contact
{
FirstName = "Darren",
LastName = "Parker",
Address1_City = "Kirkland",
FamilyStatusCode = new OptionSetValue(3),
Address1_StateOrProvince = "WA",
Address1_PostalCode = "98033",
Anniversary = new DateTime(2010, 10, 5),
CreditLimit = new Money(10000),
Description = "Coho Winery",
StatusCode = new OptionSetValue(1),
AccountRoleCode = new OptionSetValue(2),
NumberOfChildren = 2,
Address1_Latitude = 47.6105556,
Address1_Longitude = -122.1994444,
CreditOnHold = false
};
_contactId3 = _serviceProxy.Create(contact3);
Console.Write("Created a sample contact 3: {0}, ", contact3.FirstName + " " + contact3.LastName);
Contact contact4 = new Contact
{
FirstName = "Ben",
LastName = "Smith",
Address1_City = "Kirkland",
FamilyStatusCode = new OptionSetValue(3),
Address1_StateOrProvince = "WA",
Address1_PostalCode = "98033",
Anniversary = new DateTime(2010, 7, 5),
CreditLimit = new Money(12000),
Description = "Coho Winery",
StatusCode = new OptionSetValue(1),
AccountRoleCode = new OptionSetValue(2),
NumberOfChildren = 2,
Address1_Latitude = 47.6105556,
Address1_Longitude = -122.1994444,
CreditOnHold = true
};
_contactId4 = _serviceProxy.Create(contact4);
Console.Write("Created a sample contact 4: {0}, ", contact4.FirstName + " " + contact4.LastName);
Incident incident1 = new Incident
{
Title = "Test Case 1",
PriorityCode = new OptionSetValue(1), // 1 = High
CaseOriginCode = new OptionSetValue(1), // 1 = Phone
CaseTypeCode = new OptionSetValue(2), // 2 = Problem
Description = "Description for Test Case 1.",
FollowupBy = DateTime.Now.AddHours(3.0), // follow-up in 3 hours
CustomerId = new EntityReference(Contact.EntityLogicalName, _contactId2)
};
_incidentId1 = _serviceProxy.Create(incident1);
Console.Write("Created a sample incident 1: {0}, ", incident1.Title);
Relationship relationship1 = new Relationship("incident_customer_contacts");
EntityReferenceCollection relatedEntities1 = new EntityReferenceCollection();
relatedEntities1.Add(new EntityReference(Contact.EntityLogicalName, _contactId1));
_serviceProxy.Associate(Incident.EntityLogicalName, _incidentId1, relationship1, relatedEntities1);
Console.Write("Added relationship between incident 1 and contact 1, ");
Account account1 = new Account
{
Name = "Coho Winery",
Address1_Name = "Coho Vineyard & Winery",
Address1_City = "Redmond"
};
_accountId1 = _serviceProxy.Create(account1);
Console.Write("Created a sample account 1: {0}, ", account1.Name);
Incident incident2 = new Incident
{
Title = "Test Case 2",
PriorityCode = new OptionSetValue(1), // 1 = High
CaseOriginCode = new OptionSetValue(1), // 1 = Phone
CaseTypeCode = new OptionSetValue(2), // 2 = Problem
Description = "Description for Sample Case 2.",
FollowupBy = DateTime.Now.AddHours(3.0), // follow-up in 3 hours
CustomerId = new EntityReference(Contact.EntityLogicalName, _contactId1)
};
_incidentId2 = _serviceProxy.Create(incident2);
Console.Write("Created a sample incident 2: {0}, ", incident2.Title);
Relationship relationship2 = new Relationship("incident_customer_accounts");
EntityReferenceCollection relatedEntities2 = new EntityReferenceCollection();
relatedEntities2.Add(new EntityReference(Account.EntityLogicalName, _accountId1));
_serviceProxy.Associate(Incident.EntityLogicalName, _incidentId2, relationship2, relatedEntities2);
Console.Write("Added relationship between incident 2 and account 1, ");
Lead lead = new Lead()
{
FirstName = "Diogo",
LastName = "Andrade"
};
_leadId = _serviceProxy.Create(lead);
Console.Write("Created a sample Lead: {0} ", lead.FirstName + " " + lead.LastName);
Account account2 = new Account
{
Name = "Contoso Ltd",
ParentAccountId = new EntityReference(Account.EntityLogicalName, _accountId1),
Address1_Name = "Contoso Pharmaceuticals",
Address1_City = "Redmond",
OriginatingLeadId = new EntityReference(Lead.EntityLogicalName, _leadId)
};
_accountId2 = _serviceProxy.Create(account2);
Console.Write("Created a sample account 2: {0}, ", account2.Name);
Relationship relationship3 = new Relationship("account_primary_contact");
EntityReferenceCollection relatedEntities3 = new EntityReferenceCollection();
relatedEntities3.Add(new EntityReference(Account.EntityLogicalName, _accountId2));
_serviceProxy.Associate(Contact.EntityLogicalName, _contactId2, relationship3, relatedEntities3);
Console.WriteLine("Added relationship between account 2 and contact 2.");
}
/// <summary>
/// Deletes any entity records that were created for this sample.
/// <param name="prompt">Indicates whether to prompt the user
/// to delete the records created in this sample.</param>
/// </summary>
public void DeleteRequiredRecords(bool prompt)
{
// The three system users that were created by this sample will continue to
// exist on your system because system users cannot be deleted in Microsoft
// Dynamics CRM. They can only be enabled or disabled.
bool toBeDeleted = true;
if (prompt)
{
// Ask the user if the created entities should be deleted.
Console.Write("\nDo you want these entity records deleted? (y/n) [y]: ");
String answer = Console.ReadLine();
if (answer.StartsWith("y") ||
answer.StartsWith("Y") ||
answer == String.Empty)
{
toBeDeleted = true;
}
else
{
toBeDeleted = false;
}
}
if (toBeDeleted)
{
// Delete all records created in this sample.
_serviceProxy.Delete(Account.EntityLogicalName, _accountId2);
_serviceProxy.Delete(Incident.EntityLogicalName, _incidentId2);
_serviceProxy.Delete(Account.EntityLogicalName, _accountId1);
_serviceProxy.Delete(Lead.EntityLogicalName, _leadId);
_serviceProxy.Delete(Incident.EntityLogicalName, _incidentId1);
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId4);
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId3);
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId2);
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId1);
Console.WriteLine("Entity record(s) have been deleted.");
}
}
#endregion How To Sample Code
#region Main method
/// <summary>
/// Standard Main() method used by most SDK samples.
/// </summary>
/// <param name="args"></param>
static public void Main(string[] args)
{
try
{
// Obtain the target organization's Web address and client logon
// credentials from the user.
ServerConnection serverConnect = new ServerConnection();
ServerConnection.Configuration config = serverConnect.GetServerConfiguration();
LINQ101 app = new LINQ101();
app.Run(config, true);
}
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp);
Console.WriteLine("Code: {0}", ex.Detail.ErrorCode);
Console.WriteLine("Message: {0}", ex.Detail.Message);
Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == ex.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
catch (System.TimeoutException ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine("Message: {0}", ex.Message);
Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
Console.WriteLine("Inner Fault: {0}",
null == ex.InnerException.Message ? "No Inner Fault" : ex.InnerException.Message);
}
catch (System.Exception ex)
{
Console.WriteLine("The application terminated with an error.");
Console.WriteLine(ex.Message);
// Display the details of the inner exception.
if (ex.InnerException != null)
{
Console.WriteLine(ex.InnerException.Message);
FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault> fe = ex.InnerException
as FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>;
if (fe != null)
{
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp);
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode);
Console.WriteLine("Message: {0}", fe.Detail.Message);
Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText);
Console.WriteLine("Inner Fault: {0}",
null == fe.Detail.InnerFault ? "No Inner Fault" : "Has Inner Fault");
}
}
}
// Additonal exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
// SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.
finally
{
Console.WriteLine("Press <Enter> to exit.");
Console.ReadLine();
}
}
#endregion Main method
}
}
Imports System.ServiceModel
Imports System.ServiceModel.Description
' These namespaces are found in the Microsoft.Xrm.Sdk.dll assembly
' located in the SDK\bin folder of the SDK download.
Imports Microsoft.Xrm.Sdk
Imports Microsoft.Xrm.Sdk.Query
Imports Microsoft.Xrm.Sdk.Client
Imports Microsoft.Xrm.Sdk.Messages
' This namespace is found in Microsoft.Crm.Sdk.Proxy.dll assembly
' found in the SDK\bin folder.
Imports Microsoft.Crm.Sdk.Messages
Namespace Microsoft.Crm.Sdk.Samples
''' <summary>
''' Demonstrates basic and complex LINQ queries used with Microsoft Dynamics CRM
''' </summary>
''' <remarks>
''' At run-time, you will be given the option to delete all the
''' database records created by this program.</remarks>
Public Class LINQ101
#Region "Class Level Members"
Private _contactId1 As Guid
Private _contactId2 As Guid
Private _contactId3 As Guid
Private _contactId4 As Guid
Private _accountId1 As Guid
Private _accountId2 As Guid
Private _incidentId1 As Guid
Private _incidentId2 As Guid
Private _leadId As Guid
Private _serviceProxy As OrganizationServiceProxy
#End Region ' Class Level Members
#Region "How To Sample Code"
''' <summary>
''' This method first connects to the Organization service. Afterwards,
''' a series of LINQ queries are performed.
''' </summary>
''' <param name="serverConfig">Contains server connection information.</param>
''' <param name="promptforDelete">When True, the user will be prompted to delete all
''' created entities.</param>
Public Sub Run(ByVal serverConfig As ServerConnection.Configuration,
ByVal promptforDelete As Boolean)
Try
' Connect to the Organization service.
' The using statement assures that the service proxy will be properly disposed.
_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig)
Using _serviceProxy
' This statement is required to enable early-bound type support.
_serviceProxy.EnableProxyTypes()
CreateRequiredRecords()
' EarlyBoundExamples START ======================================================
' *****************************************************************************
' LNQ Simple where clause 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Acounts using where clause 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_where1 = From c In svcContext.AccountSet _
Where c.Name.Contains("Contoso") _
Select c
For Each c In query_where1
Console.WriteLine(c.Name & " " & c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Accounts using where clause 1
' ======================================
' Contoso Ltd Redmond
' =====================================
Console.WriteLine()
' ****************************************************************************
' LNQ Simple where clause 2
' ****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Acounts using where clause 2")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_where2 = From c In svcContext.AccountSet _
Where c.Name.Contains("Contoso") _
Where c.Address1_City.Equals("Redmond") _
Select c
For Each c In query_where2
Console.WriteLine(c.Name & " " & c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Accounts using where clause 2
' ======================================
' Contoso Ltd Redmond
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Join and simple where clause query
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Acount and Contact Info using where clause 3")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_where3 = From c In svcContext.ContactSet _
Join a In svcContext.AccountSet _
On c.ContactId Equals a.account_primary_contact.Id _
Where a.Name.Contains("Contoso") _
Where c.LastName.Contains("Smith") _
Select New With {Key .account_name = a.Name,
Key .contact_name = c.LastName}
For Each c In query_where3
Console.WriteLine("acct: " & c.account_name & vbTab & vbTab _
& vbTab & "contact: " & c.contact_name)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Account and Contact Info using where clause
' ======================================
' acct: Contoso Ltd contact: Smith
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the Distinct operator (returns only one of duplicate elements)
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using Distinct operator")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_distinct = ( _
From c In svcContext.ContactSet _
Select c.LastName).Distinct()
For Each c In query_distinct
Console.WriteLine(c)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using Distinct operator
' ======================================
' Parker
' Smith
' Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Simple inner join 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact and Account Info Using join 1 ")
Console.WriteLine("==================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_join1 = From c In svcContext.ContactSet _
Join a In svcContext.AccountSet _
On c.ContactId Equals a.account_primary_contact.Id _
Select New With {Key c.FullName, Key c.Address1_City,
Key a.Name, Key a.Address1_Name}
For Each c In query_join1
Console.WriteLine("acct: " & c.Name & vbTab & vbTab _
& vbTab & "contact: " & c.FullName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact and Account Info Using join 1
' ==================================
' acct: Contoso Ltd contact: Brian Smith
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Multiple join 4
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact, Account, Lead Info using multiple join 4")
Console.WriteLine("==================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_join4 = From a In svcContext.AccountSet _
Join c In svcContext.ContactSet _
On a.PrimaryContactId.Id Equals c.ContactId _
Join l In svcContext.LeadSet _
On a.OriginatingLeadId.Id Equals l.LeadId _
Select New With {Key .contact_name = c.FullName,
Key .account_name = a.Name,
Key .lead_name = l.FullName}
For Each c In query_join4
Console.WriteLine(c.contact_name & " " & c.account_name _
& " " & c.lead_name)
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' List of Contact, Account, Lead Info using multiple join 4
' ==================================
' Brian Smith Contoso Ltd Diogo Andrade
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Self join 5
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Account Info using self join 5")
Console.WriteLine("==================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_join5 = From a In svcContext.AccountSet _
Join a2 In svcContext.AccountSet _
On a.ParentAccountId.Id Equals a2.AccountId _
Select New With {
Key .account_name = a.Name,
Key .account_city = a.Address1_City
}
For Each c In query_join5
Console.WriteLine(c.account_name & " " & c.account_city)
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' List of Account Info using self join 5
' ==================================
' Contoso Ltd Redmond
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Double join 6
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using double join 6")
Console.WriteLine("==================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_join6 = From c In svcContext.ContactSet _
Join a In svcContext.AccountSet _
On c.ContactId Equals a.PrimaryContactId.Id _
Join a2 In svcContext.AccountSet _
On a.ParentAccountId.Id Equals a2.AccountId _
Select New With {Key .contact_name = c.FullName,
Key .account_name = a.Name}
For Each c In query_join6
Console.WriteLine(c.contact_name & " " & c.account_name)
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' List of Contact Info using double join 6
' ==================================
' Brian Smith Contoso Ltd
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Entity Fields join 7
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Account Info using entity field join 7")
Console.WriteLine("==================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim list_join = ( _
From a In svcContext.AccountSet _
Join c In svcContext.ContactSet _
On a.PrimaryContactId.Id Equals c.ContactId _
Where a.Name.Equals("Contoso Ltd") _
And a.Address1_Name.Equals("Contoso Pharmaceuticals") _
Select a).ToList()
For Each c In list_join
Console.WriteLine("Account " & list_join(0).Name _
& " and it's primary contact " _
& list_join(0).PrimaryContactId.Id.ToString())
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' Account Contoso Ltd and it's primary contact 918228af-04a1-e011-b1b7-00155dba3818
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Left join 8
' A left join is designed to return parents with
' and without children from two sources.
' There is a correlation between parent
' and child, but no child may actually exist.
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using left join 8")
Console.WriteLine("==================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_join8 = From a In svcContext.AccountSet _
Group Join c In svcContext.ContactSet _
On a.PrimaryContactId.Id Equals c.ContactId Into gr = _
Group From c_joined In gr.DefaultIfEmpty() _
Select New With {Key .contact_name = c_joined.FullName,
Key .account_name = a.Name}
For Each c In query_join8
Console.WriteLine(c.contact_name & " " & c.account_name)
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' List of Contact Info using left join 8
' ==================================
' Coho Winery
' Brian Smith Contoso Ltd
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the Equals operator 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using Equals operator 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_equals1 = From c In svcContext.ContactSet _
Where c.FirstName.Equals("Colin") _
Select New With {Key c.FirstName,
Key c.LastName,
Key c.Address1_City}
For Each c In query_equals1
Console.WriteLine(c.FirstName & " " & c.LastName & " " _
& c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using Equals operator 1
' ======================================
' Colin Wilcox Redmond
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the Equals operator 2
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using Equals operator 2")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_equals2 = From c In svcContext.ContactSet _
Where c.FamilyStatusCode.Equals(3) _
Select New With {Key c.FirstName,
Key c.LastName,
Key c.Address1_City}
For Each c In query_equals2
Console.WriteLine(c.FirstName & " " & c.LastName & " " _
& c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using Equals operator 2
' ======================================
' Brian Smith Bellevue
' Darren Parker Kirkland
' Ben Smith Kirkland
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the not equals operator 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using not equals operator 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_ne1 = From c In svcContext.ContactSet _
Where c.Address1_City IsNot "Redmond" _
Select New With {Key c.FirstName,
Key c.LastName,
Key c.Address1_City}
For Each c In query_ne1
Console.WriteLine(c.FirstName & " " & c.LastName _
& " " & c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using not equals operator 1
' ======================================
' Brian Smith Bellevue
' Darren Parker Kirkland
' Ben Smith Kirkland
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the not equals operator 2
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using not equals operator 2")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_ne2 = From c In svcContext.ContactSet _
Where (Not c.FirstName.Equals("Colin")) _
Select New With {Key c.FirstName,
Key c.LastName,
Key c.Address1_City}
For Each c In query_ne2
Console.WriteLine(c.FirstName & " " & c.LastName & " " _
& c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using not equals operator 2
' ======================================
' Brian Smith Bellevue
' Darren Parker Kirkland
' Ben Smith Kirkland
' =====================================
Console.WriteLine()
' *****************************************************************************
' LINQ Method-based LINQ query with where clause
' *****************************************************************************
Using svcContext As New ServiceContext(_serviceProxy)
Dim methodResults = svcContext.ContactSet _
.Where(Function(a) a.LastName.Equals("Smith"))
Dim methodResults2 = svcContext.ContactSet _
.Where(Function(a) a.LastName.StartsWith("Smi"))
Console.WriteLine()
Console.WriteLine("Method query using Lambda expression")
Console.WriteLine("---------------------------------------")
For Each a In methodResults
Console.WriteLine("Name: " & a.FirstName & " " & a.LastName)
Next a
Console.WriteLine("---------------------------------------")
Console.WriteLine("Method query 2 using Lambda expression")
Console.WriteLine("---------------------------------------")
For Each a In methodResults2
Console.WriteLine("Name: " & a.Attributes("firstname").ToString() _
& " " & a.Attributes("lastname").ToString())
Next a
End Using
Console.WriteLine("---------------------------------------")
' OUTPUT:
' Method query using Lambda expression
' ---------------------------------------
' Name: Brian Smith
' Name: Ben Smith
' ---------------------------------------
' Method query 2 using Lambda expression
' ---------------------------------------
' Name: Brian Smith
' Name: Ben Smith
' ---------------------------------------
' *****************************************************************************
' LNQ Using the greater than operator 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using greater than operator 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_gt1 = From c In svcContext.ContactSet _
Where c.Anniversary > New Date(2010, 2, 5) _
Select New With {Key c.FirstName,
Key c.LastName,
Key c.Address1_City}
For Each c In query_gt1
Console.WriteLine(c.FirstName & " " & c.LastName _
& " " & c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using greater than operator 1
' ======================================
' Colin Wilcox Redmond
' Brian Smith Bellevue
' Darren Parker Kirkland
' Ben Smith Kirkland
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the greater than operator 2
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using greater than operator 2")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_gt2 = From c In svcContext.ContactSet _
Where c.CreditLimit.Value > 20000 _
Select New With {Key c.FirstName,
Key c.LastName,
Key c.Address1_City}
For Each c In query_gt2
Console.WriteLine(c.FirstName & " " & c.LastName _
& " " & c.Address1_City)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using greater than operator 2
' ======================================
' Brian Smith Bellevue
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the ge and le operators 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the ge and le operators 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_gele1 = From c In svcContext.ContactSet _
Where c.CreditLimit.Value >= 200 _
AndAlso c.CreditLimit.Value <= 400 _
Select New With {Key c.FirstName,
Key c.LastName}
For Each c In query_gele1
Console.WriteLine(c.FirstName & " " & c.LastName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the ge and le operators 1
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the contains operator 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the contains operator 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_contains1 = From c In svcContext.ContactSet _
Where c.Description.Contains("Alpine") _
Select New With {Key c.FirstName,
Key c.LastName}
For Each c In query_contains1
Console.WriteLine(c.FirstName & " " & c.LastName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the contains operator 1
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the negated contains operator 2
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the negated contains operator 2")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_contains2 = From c In svcContext.ContactSet _
Where (Not c.Description.Contains("Coho")) _
Select New With {Key c.FirstName,
Key c.LastName}
For Each c In query_contains2
Console.WriteLine(c.FirstName.ToString() & " " _
& c.LastName.ToString())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the negated contains operator 2
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the StartsWith operator 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the StartsWith operator 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_startswith1 = From c In svcContext.ContactSet _
Where c.FirstName.StartsWith("Bri") _
Select New With {Key c.FirstName,
Key c.LastName}
For Each c In query_startswith1
Console.WriteLine(c.FirstName.ToString() & " " _
& c.LastName.ToString())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the StartsWith operator 1
' ======================================
' Brian Smith
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the endswith operator 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the EndsWith operator 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_endswith1 = From c In svcContext.ContactSet _
Where c.LastName.EndsWith("cox") _
Select New With {Key c.FirstName,
Key c.LastName}
For Each c In query_endswith1
Console.WriteLine(c.FirstName.ToString() & " " _
& c.LastName.ToString())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the EndsWith operator 1
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the && and || operators 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the && and || operators 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_andor1 = From c In svcContext.ContactSet _
Where c.Address1_City.Equals("Redmond") _
OrElse c.Address1_City.Equals("Bellevue") _
AndAlso c.CreditLimit.Value >= 200 _
Select c
For Each c In query_andor1
Console.WriteLine(c.LastName.ToString() & ", " _
& c.FirstName.ToString() & " " _
& c.Address1_City.ToString() & " " _
& c.CreditLimit.Value.ToString())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the && and || operators 1
' ======================================
' Wilcox, Colin Redmond 300.0000
' Smith, Brian Bellevue 30000.0000
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the orderby operator 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the orderby operator 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_orderby1 = From c In svcContext.ContactSet _
Where (Not c.CreditLimit.Equals(Nothing)) _
Order By c.CreditLimit Descending _
Select New With {Key .limit = c.CreditLimit,
Key .first = c.FirstName,
Key .last = c.LastName}
For Each c In query_orderby1
Console.WriteLine(c.limit.Value & " " & c.last & ", " _
& c.first)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the orderby operator 1
' ======================================
' 30000.0000 Smith, Brian
' 12000.0000 Smith, Ben
' 10000.0000 Parker, Darren
' 300.0000 Wilcox, Colin
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the orderby operator 2
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the orderby operator 2")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_orderby2 = From c In svcContext.ContactSet _
Order By c.LastName Descending, _
c.FirstName Ascending _
Select New With {Key .first = c.FirstName,
Key .last = c.LastName}
For Each c In query_orderby2
Console.WriteLine(c.last & ", " & c.first)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the orderby operator 2
' ======================================
' Wilcox, Colin
' Smith, Ben
' Smith, Brian
' Parker, Darren
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the First and Single operators
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("==========================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim firstcontact As Contact = svcContext.ContactSet _
.First()
Console.WriteLine(firstcontact.LastName & ", " _
& firstcontact.FirstName _
& " is the first contact")
Console.WriteLine("==========================")
Dim singlecontact As Contact =
svcContext.ContactSet _
.Single(Function(c) c.ContactId.Value.Equals(_contactId1))
Console.WriteLine(singlecontact.LastName & ", " _
& singlecontact.FirstName _
& " is the single contact")
End Using
Console.WriteLine("======================================")
' OUTPUT:
' ==========================
' Wilcox, Colin is the first contact
' ==========================
' Wilcox, Colin is the single contact
' ======================================
Console.WriteLine()
' *****************************************************************************
' LNQ Retrieving formatted values 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("Retrieving formatted values 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim list_retrieve1 = From c In svcContext.ContactSet _
Where c.ContactId.Value.Equals(_contactId1) _
Select New With
{Key .StatusReason =
c.FormattedValues("statuscode")}
For Each c In list_retrieve1
Console.WriteLine("Status: " & c.StatusReason)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' Retrieving formatted values 1
' ======================================
' Status: Active
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the Skip and Take operators (non-paging)
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the Skip and Take operators")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_skip = ( _
From c In svcContext.ContactSet _
Where c.LastName IsNot "Parker" _
Order By c.FirstName _
Select New With {Key .last = c.LastName,
Key .first = c.FirstName}).Skip(2).Take(2)
For Each c In query_skip
Console.WriteLine(c.first & " " & c.last)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the Skip and Take operators
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the FirstOrDefault and SingleOrDefault operators
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("==========================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim firstorcontact As Contact =
svcContext.ContactSet.FirstOrDefault()
Console.WriteLine(firstorcontact.FullName & " is the first contact")
Console.WriteLine("==========================")
Dim singleorcontact As Contact =
svcContext.ContactSet _
.SingleOrDefault(Function(c) c.ContactId.Value.Equals(_contactId1))
Console.WriteLine(singleorcontact.FullName & " is the single contact")
End Using
Console.WriteLine("======================================")
' OUTPUT:
' ==========================
' Colin Wilcox is the first contact
' ==========================
' Colin Wilcox is the single contact
' ======================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a self join with a condition on the linked entity
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact Info using a self join " _
& "with a condition on the linked entity")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_joincond = From a1 In svcContext.AccountSet _
Join a2 In svcContext.AccountSet _
On a1.ParentAccountId.Id Equals a2.AccountId _
Where a2.AccountId.Value.Equals(_accountId1) _
Select New With {Key .Account = a1,
Key .Parent = a2}
For Each a In query_joincond
Console.WriteLine(a.Account.Name & " " & a.Parent.Name)
Next a
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using a self join with a condition on the linked entity
' ======================================
' Contoso Ltd Coho Winery
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a transformation in the where clause
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact Info using a transformation in the where clause")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_wheretrans = From c In svcContext.ContactSet _
Where c.ContactId.Value.Equals(_contactId1) _
AndAlso c.Anniversary > Date.Parse("1/1/2010") _
Select New With {Key c.FirstName,
Key c.LastName}
For Each c In query_wheretrans
Console.WriteLine(c.FirstName & " " & c.LastName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using a transformation in the where clause
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a lookup value to order by
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Account Info using a lookup value to order by")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_orderbylookup = From a In svcContext.AccountSet _
Where a.Address1_Name.Equals("Contoso Pharmaceuticals") _
Order By a.PrimaryContactId _
Select New With {Key a.Name,
Key a.Address1_City}
For Each a In query_orderbylookup
Console.WriteLine(a.Name & " " & a.Address1_City)
Next a
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Account Info using a lookup value to order by
' ======================================
' Contoso Ltd Redmond
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a a picklist to order by
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using a picklist to order by")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_orderbypicklist = From c In svcContext.ContactSet _
Where c.LastName IsNot "Parker" _
AndAlso c.AccountRoleCode IsNot Nothing _
Order By c.AccountRoleCode, c.FirstName _
Select New With
{Key .AccountRole =
c.FormattedValues("accountrolecode"),
Key c.FirstName, Key c.LastName}
For Each c In query_orderbypicklist
Console.WriteLine(c.AccountRole & " " & c.FirstName _
& " " & c.LastName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using a picklist to order by
' ======================================
' Decision Maker Colin Wilcox
' Employee Ben Smith
' Employee Brian Smith
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a paging sort 1
' (Multi-column sort with extra condition)
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using a paging sort 1")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_pagingsort1 = ( _
From c In svcContext.ContactSet _
Where c.LastName IsNot "Parker" _
Order By c.LastName Ascending, c.FirstName Descending _
Select New With {Key c.FirstName,
Key c.LastName}).Skip(2).Take(2)
For Each c In query_pagingsort1
Console.WriteLine(c.FirstName & " " & c.LastName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using a paging sort 1
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a paging sort 2
' (Page and sort where the column being sorted
' is different from the column being retrieved
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using a paging sort 2")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_pagingsort2 = ( _
From c In svcContext.ContactSet _
Where c.LastName IsNot "Parker" _
Order By c.FirstName Descending _
Select New With {Key c.FirstName}).Skip(2).Take(2)
For Each c In query_pagingsort2
Console.WriteLine(c.FirstName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using a paging sort 2
' ======================================
' Ben
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a paging sort 3
' (Creates only the first page)
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using a paging sort 3")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_pagingsort3 = ( _
From c In svcContext.ContactSet _
Where c.LastName.StartsWith("W") _
Order By c.MiddleName Ascending, c.FirstName Descending _
Select New With {Key c.FirstName,
Key c.MiddleName,
Key c.LastName}).Take(10)
For Each c In query_pagingsort3
Console.WriteLine(c.FirstName & " " & c.MiddleName & " " & c.LastName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using a paging sort 3
' ======================================
' Colin Wilcox
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Retrieving related entity columns (for 1 to N relationships)
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact and Account Info by retrieving related entity columns")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_retrieve1 = From c In svcContext.ContactSet _
Join a In svcContext.AccountSet _
On c.ContactId Equals a.PrimaryContactId.Id _
Where Not c.ContactId.Value.Equals(_contactId1) _
Select New With {Key .Contact = c,
Key .Account = a}
For Each c In query_retrieve1
Console.WriteLine("Acct: " & c.Account.Name & vbTab & vbTab _
& "Contact: " & c.Contact.FullName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact and Account Info by retrieving related entity columns
' ======================================
' Acct: Contoso Ltd Contact: Brian Smith
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using .Value to retrieve the value of an attribute
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact Info using .Value to retrieve the value of an attribute")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_value = From c In svcContext.ContactSet _
Where Not c.ContactId.Value.Equals(_contactId2) _
Select New With
{Key .ContactId = If(
c.ContactId IsNot Nothing,
c.ContactId.Value,
Guid.Empty),
Key .NumberOfChildren = If(
c.NumberOfChildren IsNot Nothing,
c.NumberOfChildren.Value, Nothing),
Key .CreditOnHold = If(
c.CreditOnHold IsNot Nothing,
c.CreditOnHold.Value,
Nothing),
Key .Anniversary = If(
c.Anniversary IsNot Nothing,
c.Anniversary.Value,
Nothing)}
For Each c In query_value
Console.WriteLine(c.ContactId.ToString() & " " _
& c.NumberOfChildren & " " _
& c.CreditOnHold & " " & c.Anniversary)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using .Value to retrieve the value of an attribute
' ======================================
' 8f8228af-04a1-e011-b1b7-00155dba3818 1 False 3/5/2010 8:00:00 AM
' 938228af-04a1-e011-b1b7-00155dba3818 2 False 10/5/2010 7:00:00 AM
' 958228af-04a1-e011-b1b7-00155dba3818 2 True 7/5/2010 7:00:00 AM
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Multiple projections, new data type casting to different types
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using multiple projections")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_projections = From c In svcContext.ContactSet _
Where c.ContactId.Value.Equals(_contactId1) _
And Not c.NumberOfChildren.Equals(Nothing) _
And Not c.Anniversary.Equals(Nothing) _
Select New With
{Key .Contact =
New Contact With
{.LastName = c.LastName,
.NumberOfChildren = c.NumberOfChildren},
Key .NumberOfChildren = CDbl(c.NumberOfChildren),
Key .Anniversary = c.Anniversary.Value.AddYears(1)}
For Each c In query_projections
Console.WriteLine(c.Contact.LastName & " " & c.NumberOfChildren _
& " " & c.Anniversary)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using multiple projections
' ======================================
' Wilcox 1 3/5/2011 8:00:00 AM
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the GetAttributeValue method
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using the GetAttributeValue method")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_getattrib = From c In svcContext.ContactSet _
Where Not c.GetAttributeValue(Of Guid)("contactid").Equals( _
_contactId1) _
Select New With
{Key .ContactId =
c.GetAttributeValue(Of Guid?)("contactid"),
Key .NumberOfChildren =
c.GetAttributeValue(Of Integer?)("numberofchildren"),
Key .CreditOnHold =
c.GetAttributeValue(Of Boolean?)("creditonhold"),
Key .Anniversary =
c.GetAttributeValue(Of Date?)("anniversary")}
For Each c In query_getattrib
Console.WriteLine(c.ContactId.ToString() & " " _
& c.NumberOfChildren & " " & c.CreditOnHold _
& " " & c.Anniversary)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using the GetAttributeValue method
' ======================================
' 918228af-04a1-e011-b1b7-00155dba3818 2 False 4/5/2010 7:00:00 AM
' 938228af-04a1-e011-b1b7-00155dba3818 2 False 10/5/2010 7:00:00 AM
' 958228af-04a1-e011-b1b7-00155dba3818 2 True 7/5/2010 7:00:00 AM
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using math operations
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using math operations")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_math = From c In svcContext.ContactSet _
Where Not c.ContactId.Value.Equals(_contactId2) _
AndAlso c.Address1_Latitude IsNot Nothing _
AndAlso c.Address1_Longitude IsNot Nothing _
Select New With
{Key .Round =
Math.Round(c.Address1_Latitude.Value),
Key .Floor =
Math.Floor(c.Address1_Latitude.Value),
Key .Ceiling =
Math.Ceiling(c.Address1_Latitude.Value),
Key .Abs =
Math.Abs(c.Address1_Latitude.Value)}
For Each c In query_math
Console.WriteLine(c.Round & " " & c.Floor & " " _
& c.Ceiling & " " & c.Abs)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using math operations
' ======================================
' 48 47 48 47.67417
' 48 47 48 47.61056
' 48 47 48 47.61056
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using multiple select and where clauses
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Incidents using multiple select and where clauses")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_multiselect =
svcContext.IncidentSet _
.Where(Function(i) Not i.IncidentId.Value.Equals(_incidentId1)) _
.Select(Function(i) i.incident_customer_accounts) _
.Where(Function(a) Not a.AccountId.Value.Equals(_accountId2)) _
.Select(Function(a) a.account_primary_contact) _
.OrderBy(Function(c) c.FirstName).Select(Function(c) c.ContactId)
For Each c In query_multiselect
Console.WriteLine(c.GetValueOrDefault())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Incidents using multiple select and where clauses
' ======================================
' 918228af-04a1-e011-b1b7-00155dba3818
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using SelectMany
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Account Info using SelectMany")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_selectmany =
svcContext.ContactSet _
.Where(Function(c) Not c.ContactId.Value.Equals(_contactId2)) _
.SelectMany(Function(c) c.account_primary_contact) _
.OrderBy(Function(a) a.Name)
For Each c In query_selectmany
Console.WriteLine(c.AccountId.ToString() & " " & c.Name)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Account Info using SelectMany
' ======================================
' 9b8228af-04a1-e011-b1b7-00155dba3818 Contoso Ltd
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using String operations
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using String operations")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_string = From c In svcContext.ContactSet _
Where c.ContactId.Value.Equals(_contactId2) _
Select New With
{Key .IndexOf = c.FirstName.IndexOf("contact"),
Key .Insert = c.FirstName.Insert(1, "Insert"),
Key .Remove = c.FirstName.Remove(1, 1),
Key .Substring = c.FirstName.Substring(1, 1),
Key .ToUpper = c.FirstName.ToUpper(),
Key .ToLower = c.FirstName.ToLower(),
Key .TrimStart = c.FirstName.TrimStart(),
Key .TrimEnd = c.FirstName.TrimEnd()}
For Each c In query_string
Console.WriteLine(c.IndexOf & vbLf & c.Insert & vbLf _
& c.Remove & vbLf & c.Substring & vbLf _
& c.ToUpper & vbLf & c.ToLower & vbLf _
& c.TrimStart & " " & c.TrimEnd)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using String operations
' ======================================
' -1
' BInsertrian
' Bian
' r
' BRIAN
' brian
' Brian Brian
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using Two Where Clauses
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Account and Contact Info using two where clauses")
Console.WriteLine("======================================")
Using svcContext As New ServiceContext(_serviceProxy)
Dim query_twowhere = From a In svcContext.AccountSet _
Join c In svcContext.ContactSet _
On a.PrimaryContactId.Id Equals c.ContactId _
Where c.LastName.Equals("Smith") _
AndAlso c.CreditOnHold IsNot Nothing _
Where a.Name.Equals("Contoso Ltd") _
Order By a.Name _
Select a
For Each c In query_twowhere
Console.WriteLine(c.AccountId.ToString() & " " & c.Name)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Account and Contact Info using two where clauses
' ======================================
' 9b8228af-04a1-e011-b1b7-00155dba3818 Contoso Ltd
' =====================================
Console.WriteLine()
' EarlyBoundExamples END ======================================================
' LateBoundExamples START ======================================================
' *****************************************************************************
' LNQ Using late-bound entities, simple inner join 2
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact and Account Info using late-bound entities with join 2")
Console.WriteLine("==================================")
Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
Dim query_join2 = From c In orgSvcContext.CreateQuery("contact") _
Join a In orgSvcContext.CreateQuery("account") _
On c("contactid") Equals a("primarycontactid") _
Select New With {Key .contact_name = c("fullname"),
Key .account_name = a("name")}
For Each c In query_join2
Console.WriteLine(c.contact_name.ToString() & " " _
& c.account_name.ToString())
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' List of Contact and Account Info using late-bound entities with join 2
' ==================================
' Brian Smith Contoso Ltd
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Alternative syntax simple inner join 3
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact and Account Info with alternative syntax join 3")
Console.WriteLine("==================================")
Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
Dim query_join3 = From c In orgSvcContext.CreateQuery("contact") _
Join a In orgSvcContext.CreateQuery("account") _
On c("contactid") Equals a("primarycontactid") _
Select New With {Key .contact_name = c("fullname"),
Key .account_name = a("name")}
For Each c In query_join3
Console.WriteLine(c.contact_name.ToString() & " " _
& c.account_name.ToString())
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' List of Contact and Account Info with alternative syntax join 3
' ==================================
' Brian Smith Contoso Ltd
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Late binding left join 9
' A left join is designed to return parents with
' and without children from two sources.
' There is a correlation between parent
' and child, but no child may actually exist.
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Contact Info using late binding left join 9")
Console.WriteLine("==================================")
Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
Dim query_join9 = From a In orgSvcContext.CreateQuery("account") _
Group Join c In orgSvcContext.CreateQuery("contact") _
On a("primarycontactid") Equals c("contactid") Into gr = _
Group From c_joined In gr.DefaultIfEmpty() _
Select New With {Key .account_name = a.Attributes("name")}
For Each c In query_join9
Console.WriteLine(c.account_name)
Next c
End Using
Console.WriteLine("==================================")
' OUTPUT:
' List of Contact Info using late binding left join 9
' ==================================
' Coho Winery
' Contoso Ltd
' ==================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using late binding and not equals operator 3
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact Info using late binding and not equals operator 3")
Console.WriteLine("======================================")
Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
Dim query_ne3 = From c In orgSvcContext.CreateQuery("contact") _
Where (Not c("address1_city").Equals(Nothing)) _
Select New With {Key .FirstName = c("firstname"),
Key .LastName = c("lastname"),
Key .Address1_City =
c("address1_city")}
For Each c In query_ne3
Console.WriteLine(c.FirstName.ToString() & " " _
& c.LastName.ToString() & " " _
& c.Address1_City.ToString())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using late binding and not equals operator 3
' ======================================
' Colin Wilcox Redmond
' Brian Smith Bellevue
' Darren Parker Kirkland
' Ben Smith Kirkland
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using late binding with Contains operator 3
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact Info using late binding with Contains operator 3")
Console.WriteLine("======================================")
Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
Dim query_contains3 = From c In orgSvcContext.CreateQuery("contact") _
Where (CStr(c("description"))).Contains("Coho") _
Select New With
{Key .firstname = c.Attributes("firstname"),
Key .lastname = c.Attributes("lastname")}
For Each c In query_contains3
Console.WriteLine(c.firstname.ToString() & " " _
& c.lastname.ToString())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using late binding with Contains operator 3
' ======================================
' Brian Smith
' Darren Parker
' Ben Smith
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using the GetAttributeValue (late binding) method 1
' *****************************************************************************
Console.WriteLine()
Console.WriteLine(
"List of Contact Info using late binding " _
& "and the GetAttributeValue method 2")
Console.WriteLine("======================================")
Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
Dim list_getattrib1 = ( _
From c In orgSvcContext.CreateQuery("contact") _
Where Not c.GetAttributeValue(Of Guid?)("contactid") _
.Value.Equals(_contactId1) _
Select New With
{
Key .FirstName = c.GetAttributeValue(Of String)("firstname"),
Key .LastName = c.GetAttributeValue(Of String)("lastname")}
).ToList()
For Each c In list_getattrib1
Console.WriteLine(c.FirstName & " " & c.LastName)
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Contact Info using late binding and the GetAttributeValue method 2
' ======================================
' Brian Smith
' Darren Parker
' Ben Smith
' =====================================
Console.WriteLine()
' *****************************************************************************
' LNQ Using a late binding join
' *****************************************************************************
Console.WriteLine()
Console.WriteLine("List of Info using a late binding join")
Console.WriteLine("======================================")
'Note: Where statements are not working with the below multiple join query.
' Refer equivalent C# snippet for full complex query.
'TODO: Fix issue to include where statements as showin in C# sample.
Using orgSvcContext As New OrganizationServiceContext(_serviceProxy)
Dim query_dejoin = From c In orgSvcContext.CreateQuery("contact") _
Join a In orgSvcContext.CreateQuery("account")
On c("contactid") Equals a("primarycontactid") _
Join l In orgSvcContext.CreateQuery("lead") _
On a("originatingleadid") Equals l("leadid") _
Select New With {Key .Contact = c,
Key .Account = a,
Key .Lead = l}
For Each c In query_dejoin
Console.WriteLine(c.Account.Attributes("name").ToString() _
& " " _
& c.Contact.Attributes("fullname").ToString() _
& " " _
& c.Lead.Attributes("leadid").ToString())
Next c
End Using
Console.WriteLine("=====================================")
' OUTPUT:
' List of Info using a late binding join
' ======================================
' Contoso Ltd Brian Smith 9a8228af-04a1-e011-b1b7-00155dba3818
' =====================================
Console.WriteLine()
' LateBoundExamples END ======================================================
DeleteRequiredRecords(promptforDelete)
End Using
' Catch any service fault exceptions that Microsoft Dynamics CRM throws.
Catch fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
' You can handle an exception here or pass it back to the calling method.
Throw
End Try
End Sub
''' <summary>
''' Creates any entity records that this sample requires.
''' </summary>
Public Sub CreateRequiredRecords()
Dim contact1 As Contact =
New Contact With
{.FirstName = "Colin",
.LastName = "Wilcox",
.Address1_City = "Redmond",
.Address1_StateOrProvince = "WA",
.Address1_PostalCode = "98052",
.Anniversary = New Date(2010, 3, 5),
.CreditLimit = New Money(300),
.Description = "Alpine Ski House",
.StatusCode = New OptionSetValue(1),
.AccountRoleCode = New OptionSetValue(1),
.NumberOfChildren = 1,
.Address1_Latitude = 47.6741667,
.Address1_Longitude = -122.1202778,
.CreditOnHold = False}
_contactId1 = _serviceProxy.Create(contact1)
Console.Write("Created a sample contact 1: {0}, ",
contact1.FirstName & " " & contact1.LastName)
Dim contact2 As Contact = New Contact With
{.FirstName = "Brian",
.LastName = "Smith",
.Address1_City = "Bellevue",
.FamilyStatusCode = New OptionSetValue(3),
.Address1_StateOrProvince = "WA",
.Address1_PostalCode = "98008",
.Anniversary = New Date(2010, 4, 5),
.CreditLimit = New Money(30000),
.Description = "Coho Winery",
.StatusCode = New OptionSetValue(1),
.AccountRoleCode = New OptionSetValue(2),
.NumberOfChildren = 2,
.Address1_Latitude = 47.6105556,
.Address1_Longitude = -122.1994444,
.CreditOnHold = False}
_contactId2 = _serviceProxy.Create(contact2)
Console.Write("Created a sample contact 2: {0}, ",
contact2.FirstName & " " & contact2.LastName)
Dim contact3 As Contact = New Contact With
{.FirstName = "Darren",
.LastName = "Parker",
.Address1_City = "Kirkland",
.FamilyStatusCode = New OptionSetValue(3),
.Address1_StateOrProvince = "WA",
.Address1_PostalCode = "98033",
.Anniversary = New Date(2010, 10, 5),
.CreditLimit = New Money(10000),
.Description = "Coho Winery",
.StatusCode = New OptionSetValue(1),
.AccountRoleCode = New OptionSetValue(2),
.NumberOfChildren = 2,
.Address1_Latitude = 47.6105556,
.Address1_Longitude = -122.1994444,
.CreditOnHold = False}
_contactId3 = _serviceProxy.Create(contact3)
Console.Write("Created a sample contact 3: {0}, ",
contact3.FirstName & " " & contact3.LastName)
Dim contact4 As Contact = New Contact With
{.FirstName = "Ben",
.LastName = "Smith",
.Address1_City = "Kirkland",
.FamilyStatusCode = New OptionSetValue(3),
.Address1_StateOrProvince = "WA",
.Address1_PostalCode = "98033",
.Anniversary = New Date(2010, 7, 5),
.CreditLimit = New Money(12000),
.Description = "Coho Winery",
.StatusCode = New OptionSetValue(1),
.AccountRoleCode = New OptionSetValue(2),
.NumberOfChildren = 2,
.Address1_Latitude = 47.6105556,
.Address1_Longitude = -122.1994444,
.CreditOnHold = True}
_contactId4 = _serviceProxy.Create(contact4)
Console.Write("Created a sample contact 3: {0}, ",
contact4.FirstName & " " & contact4.LastName)
Dim incident1 As Incident = New Incident With
{.Title = "Test Case 1",
.PriorityCode = New OptionSetValue(1),
.CaseOriginCode = New OptionSetValue(1),
.CaseTypeCode = New OptionSetValue(2),
.Description = "Description for Test Case 1.",
.FollowupBy = Date.Now.AddHours(3.0),
.CustomerId =
New EntityReference(Contact.EntityLogicalName,
_contactId2)}
_incidentId1 = _serviceProxy.Create(incident1)
Console.Write("Created a sample incident 1: {0}, ", incident1.Title)
Dim relationship1 As New Relationship("incident_customer_contacts")
Dim relatedEntities1 As New EntityReferenceCollection()
relatedEntities1.Add(New EntityReference(Contact.EntityLogicalName,
_contactId1))
_serviceProxy.Associate(Incident.EntityLogicalName, _incidentId1,
relationship1, relatedEntities1)
Console.Write("Added relationship between incident 1 and contact 1, ")
Dim account1 As Account = New Account With
{.Name = "Coho Winery",
.Address1_Name = "Coho Vineyard & Winery",
.Address1_City = "Redmond"}
_accountId1 = _serviceProxy.Create(account1)
Console.Write("Created a sample account 1: {0}, ", account1.Name)
Dim incident2 As Incident = New Incident With
{.Title = "Test Case 2",
.PriorityCode = New OptionSetValue(1),
.CaseOriginCode = New OptionSetValue(1),
.CaseTypeCode = New OptionSetValue(2),
.Description = "Description for Sample Case 2.",
.FollowupBy = Date.Now.AddHours(3.0),
.CustomerId = New EntityReference(
Contact.EntityLogicalName,
_contactId1)}
_incidentId2 = _serviceProxy.Create(incident2)
Console.Write("Created a sample incident 2: {0}, ", incident2.Title)
Dim relationship2 As New Relationship("incident_customer_accounts")
Dim relatedEntities2 As New EntityReferenceCollection()
relatedEntities2.Add(New EntityReference(Account.EntityLogicalName,
_accountId1))
_serviceProxy.Associate(Incident.EntityLogicalName, _incidentId2,
relationship2, relatedEntities2)
Console.Write("Added relationship between incident 2 and account 1, ")
Dim lead As New Lead() With {.FirstName = "Diogo", .LastName = "Andrade"}
_leadId = _serviceProxy.Create(lead)
Console.Write("Created a sample Lead: {0} ", lead.FirstName & " " & lead.LastName)
Dim account2 As Account = New Account With
{.Name = "Contoso Ltd",
.ParentAccountId =
New EntityReference(Account.EntityLogicalName,
_accountId1),
.Address1_Name = "Contoso Pharmaceuticals",
.Address1_City = "Redmond",
.OriginatingLeadId =
New EntityReference(lead.EntityLogicalName,
_leadId)}
_accountId2 = _serviceProxy.Create(account2)
Console.Write("Created a sample account 2: {0}, ", account2.Name)
Dim relationship3 As New Relationship("account_primary_contact")
Dim relatedEntities3 As New EntityReferenceCollection()
relatedEntities3.Add(New EntityReference(Account.EntityLogicalName,
_accountId2))
_serviceProxy.Associate(Contact.EntityLogicalName, _contactId2,
relationship3, relatedEntities3)
Console.WriteLine("Added relationship between account 2 and contact 2.")
End Sub
''' <summary>
''' Deletes any entity records that were created for this sample.
''' <param name="prompt">Indicates whether to prompt the user
''' to delete the records created in this sample.</param>
''' </summary>
Public Sub DeleteRequiredRecords(ByVal prompt As Boolean)
' The three system users that were created by this sample will continue to
' exist on your system because system users cannot be deleted in Microsoft
' Dynamics CRM. They can only be enabled or disabled.
Dim toBeDeleted As Boolean = True
If prompt Then
' Ask the user if the created entities should be deleted.
Console.Write(vbLf _
& "Do you want these entity records deleted? (y/n) [y]: ")
Dim answer As String = Console.ReadLine()
If answer.StartsWith("y") OrElse answer.StartsWith("Y") _
OrElse answer = String.Empty Then
toBeDeleted = True
Else
toBeDeleted = False
End If
End If
If toBeDeleted Then
' Delete all records created in this sample.
_serviceProxy.Delete(Account.EntityLogicalName, _accountId2)
_serviceProxy.Delete(Incident.EntityLogicalName, _incidentId2)
_serviceProxy.Delete(Account.EntityLogicalName, _accountId1)
_serviceProxy.Delete(Lead.EntityLogicalName, _leadId)
_serviceProxy.Delete(Incident.EntityLogicalName, _incidentId1)
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId4)
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId3)
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId2)
_serviceProxy.Delete(Contact.EntityLogicalName, _contactId1)
Console.WriteLine("Entity record(s) have been deleted.")
End If
End Sub
#End Region ' How To Sample Code
#Region "Main method"
''' <summary>
''' Standard Main() method used by most SDK samples.
''' </summary>
''' <param name="args"></param>
Public Shared Sub Main(ByVal args() As String)
Try
' Obtain the target organization's Web address and client logon
' credentials from the user.
Dim serverConnect As New ServerConnection()
Dim config As ServerConnection.Configuration = serverConnect.GetServerConfiguration()
Dim app As New LINQ101()
app.Run(config, True)
Catch ex As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault)
Console.WriteLine("The application terminated with an error.")
Console.WriteLine("Timestamp: {0}", ex.Detail.Timestamp)
Console.WriteLine("Code: {0}", ex.Detail.ErrorCode)
Console.WriteLine("Message: {0}", ex.Detail.Message)
Console.WriteLine("Plugin Trace: {0}", ex.Detail.TraceText)
Console.WriteLine("Inner Fault: {0}", If(Nothing Is ex.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
Catch ex As TimeoutException
Console.WriteLine("The application terminated with an error.")
Console.WriteLine("Message: {0}", ex.Message)
Console.WriteLine("Stack Trace: {0}", ex.StackTrace)
Console.WriteLine("Inner Fault: {0}", If(Nothing Is ex.InnerException.Message, "No Inner Fault", ex.InnerException.Message))
Catch ex As Exception
Console.WriteLine("The application terminated with an error.")
Console.WriteLine(ex.Message)
' Display the details of the inner exception.
If ex.InnerException IsNot Nothing Then
Console.WriteLine(ex.InnerException.Message)
Dim fe As FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault) =
TryCast(ex.InnerException,
FaultException(Of Microsoft.Xrm.Sdk.OrganizationServiceFault))
If fe IsNot Nothing Then
Console.WriteLine("Timestamp: {0}", fe.Detail.Timestamp)
Console.WriteLine("Code: {0}", fe.Detail.ErrorCode)
Console.WriteLine("Message: {0}", fe.Detail.Message)
Console.WriteLine("Plugin Trace: {0}", fe.Detail.TraceText)
Console.WriteLine("Inner Fault: {0}", If(Nothing Is fe.Detail.InnerFault, "No Inner Fault", "Has Inner Fault"))
End If
End If
' Additonal exceptions to catch: SecurityTokenValidationException, ExpiredSecurityTokenException,
' SecurityAccessDeniedException, MessageSecurityException, and SecurityNegotiationException.
Finally
Console.WriteLine("Press <Enter> to exit.")
Console.ReadLine()
End Try
End Sub
#End Region ' Main method
End Class
End Namespace
See Also
OrganizationServiceContext
Build queries with LINQ (.NET language-integrated query)
Sample: RetrieveMultiple with condition operators using LINQ