Using LINQ in CRM 2013


There are few ways we can make use of LINQ to write queries against CRM.

Using the early bound entity classes along with the generated service context

Use the CrmSvcUtil tool to generated the early bound entity classes and service context

CrmSvcUtil.exe /url:http://<serverName>/<organizationName>/XRMServices/2011/Organization.svc

/out:<outputFilename>.cs /username:<username> /password:<password> /domain:<domainName>

/namespace:<outputNamespace> /serviceContextName:<serviceContextName>

 

Uri organizationUri = new Uri("http://server/orgname/XRMServices/2011/Organization.svc");
 Uri homeRealmUri = null;</pre>
<pre> ClientCredentials credentials = new ClientCredentials();
 credentials.Windows.ClientCredential = new System.Net.NetworkCredential("username", "password", "domain");
 OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
 orgProxy.EnableProxyTypes();
 IOrganizationService _service = (IOrganizationService)orgProxy; 
 
 var xrmServiceContext = new XrmServiceContext(_service);
 // get all contact record that has first name as Hugh
 var lstContact = (from c in xrmServiceContext.ContactSet
 where c.FirstName == "Hugh"
 select c).ToList();
 foreach(var contact in lstContact)
 {
 MessageBox.Show(contact.FullName);
 }


Using Early bound entity classes and OrganizationServiceContext

Suppose we have the early bound entity classes but haven’t generated the service context. In this case we can use OrganizationServiceContext

 


var xrmServiceContext = new OrganizationServiceContext(_service);

var lstContact = (from c in xrmServiceContext.CreateQuery<Contact>()
 where c.FirstName == "Hugh"
 select c).ToList();

foreach (var contact in lstContact)
 {
 MessageBox.Show(contact.FullName);
 }

Using Late bound and OrganizationServiceContext

Suppose we are not generating the early bound classes and using late binding

 

 


  var xrmServiceContext = new OrganizationServiceContext(_service);

 var lstContact = (from c in xrmServiceContext.CreateQuery("contact")
 where c["firstname"] == "Hugh"
 select c).ToList();

 foreach (var contact in lstContact)
 {
 MessageBox.Show(contact["fullname"].ToString());
 }

Hope it helps..

 

Dll not getting copied to bin directory in Visual Studio


Hi,

In one of our projects we were referencing custom assemblies that were part of the same solution. However those assemblies were not getting copied to the bin directory of the project upon building the project.

We had also set property Copy Local as true for those class library projects.

The reason for this was as we were adding those assemblies in GAC.

VS.NET will copy the dll to the bin directory if your system cannot find the dll in the GAC.”

After removing those assemblies from GAC it added those dlls in the bin.

The helpful thread

http://stackoverflow.com/questions/3548731/visual-studio-2010-add-reference-is-copying-dll-to-bin-directory

Hope it helps..

“the specified type is not a known entity type” error while using early bound entity classes in CRM 2013


Hi,

We were writing a custom workflow activity that was using LINQ (early bound).

Here we had used ILMerge to merge the early bound assemblies with the workflow assembly.

The solution was to place the following attribute in the AssemblyInfo.cs of the workflow assembly (same goes for Plugin Assembly)

[assembly: Microsoft.Xrm.Sdk.Client.ProxyTypesAssemblyAttribute()]

 

The helpful thread

http://social.microsoft.com/Forums/en-US/7c05f900-f930-46b3-a233-75a94ffca9c5/how-to-make-the-crm-2011-plugin-detect-types-from-early-bound-organisation-class?forum=crmdevelopment

Hope it helps..

 

 

How to – Remove duplicate objects in list in C#


Just sharing a sample code to remove duplicates from a list using LINQ

public class MyClass
{
public string ID { get; set; }
public string Value { get; set; }

}

List<MyClass> myList = new List<MyClass>();
var xrmOptionSet = new MyClass();
xrmOptionSet.ID = "1";
xrmOptionSet.Value = "100";
var xrmOptionSet1 = new MyClass();
xrmOptionSet1.ID = "2";
xrmOptionSet1.Value = "200";
var xrmOptionSet2 = new MyClass();
xrmOptionSet2.ID = "1";
xrmOptionSet2.Value = "100";
myList.Add(xrmOptionSet);
myList.Add(xrmOptionSet1);
myList.Add(xrmOptionSet2);

// here we are first grouping the result by label and then picking the first item from each group
var myDistinctList = myList.GroupBy(i => i.ID)
.Select(g => g.First()).ToList();

Hope it helps..

Load your programming emulators on the cloud & access it conveniently on virtual desktops with your preferred mobile device (iOS/Android/windows) remotely with CloudDesktopOnline.com. For effective team collaboration use a hosted SharePoint and exchange from Apps4Rent.

Advertisements

Specifying New Line in XML


Hi,

While specifying Data for one of the multiline column in a ListInstance we wanted to specify New Line in the content.

This is how we specified

The result

Hope it helps..

Fixed – Cannot obtain value of local or argument as it not available at the instruction pointer, possibly because it has been optimized away while debugging in Visual Studio


Hi,

Was facing this the above issue while trying to debug the code. The debugger was getting attached properly but it wasn’t showing values for the variables.
Unchecking the following option in the class library project properties fixed the issue

 

 

This helpful post.

http://blogs.msdn.com/b/sburke/archive/2008/01/29/how-to-disable-optimizations-when-debugging-reference-source.aspx

Bye..

 

Advertisements