Hi,
Check out these wonderful posts !
http://weblogs.asp.net/scottgu/archive/2009/08/25/vs-2010-and-net-4-series.aspx
Bye..
Hi,
Check out these wonderful posts !
http://weblogs.asp.net/scottgu/archive/2009/08/25/vs-2010-and-net-4-series.aspx
Bye..
Suppose this is our class Person, having an string property name FullName.
}
private void button1_Click(object sender, RoutedEventArgs e)
{
UnderstandingPassbyValandRef();
}
private void UnderstandingPassbyValandRef()
{
Person myPOriginal = new Person();
myPOriginal.FullName = "Nishant Rana";
PassByValue(myPOriginal);
MessageBox.Show(myPOriginal.FullName);
// Output : “Nishant Rana PassByVal”
PassByRef(ref myPOriginal);
MessageBox.Show(myPOriginal.FullName);
// Output : “Srihari Radhakrishnan”
this.Close();
}
Bye..
Suppose this is our entity’s schema name “new_test”
and it contains following fields
new_name
new_lastname
Using CrmSvcUtil we have created the Entities classes and Data Context class.
https://nishantrana.wordpress.com/2010/08/11/using-crmsvcutil/
Now this is how we could use LINQ to query data using query expression as well as method based query.
To loop through all the test records
To select a specific record
var singleRecord1 = myXrm.new_tests
.Single(t => t.new_lastname == “Rana”);
var allRecords1 = myXrm.new_tests.
Where(t => t.new_lastname == “Rana”);
To order the records
var allRecOrder1 = myXrm.new_tests
.OrderBy(t => t.new_name)
.Where(t => t.new_lastname == “Rana”);
To select specific field instead of the entire record
var singleField1 = myXrm.new_tests
.Select(t => t.new_name);
To return specific fields
var specificFields = from myTest in myXrm.new_tests
select new { myTest.createdby, myTest.createdon };
var specificFields1 = myXrm.new_tests
.Select(t => new { t.createdby, t.createdon });
Use of Take and Skip function
Take returns the given number of elements and ignores the rest
Skip skips the given number of elements and yielding the rest
var takeField = (from myTest in myXrm.new_tests
select myTest.new_name).Take(2);
var takeField1 = myXrm.new_tests
.Take(2)
.Select(t => t.new_name);
var skipField = (from myTest in myXrm.new_tests
select myTest.new_name).Skip(2);
var skipField1 = myXrm.new_tests
.Skip(2)
.Select(t => t.new_name);
Join – similar to inner join
The ‘select’ and ‘orderBy’ calls may only reference
var joinRecords1=myXrm.new_tests
.Join(myXrm.systemusers,
t=>t.ownerid.Value,
s=>s.systemuserid ,
(t,s)=>new {t.new_name});
Where conditions with Contains,StartsWith, EndsWith and
var test11 = myXrm.new_tests
.Where(t => t.new_name.Contains(“R”));
var test22 = myXrm.new_tests
.Where(t => t.new_name.StartsWith(“R”));
var test32 = myXrm.new_tests
.Where(t => t.new_name.EndsWith(“D”));
Download the project :-
http://www.box.net/shared/xbg0xd5p7m
Bye..
Use CrmSvcUtil to generate the strongly types entity and datacontext classes
crmsvcutil
/connectionString:"Authentication Type=Integrated; Server=http://servername/orgname"
/out:"Xrm.cs" /namespace:"Xrm.solution"
/dataContextClassName:"xrm"
conntectionString–> specifies the connection string.
out –> determines the name of the .cs or .xml output file and whether there is one file or one per entity.To generate one file per entity omit .cs or .xml from the file name.
namespace –> to specify the namespace
dataContextClassName –> to specify the name of the dataContext class.
Add references to the following dlls in the project
Add the generated Xrm.cs file to the project.
Add the connection string information in the config file of the application
<add name="CRMOnPremise" connectionString="Authentication Type=Integrated; Server=http://servername/orgname" />
Add following using Statements
using Xrm;
using Xrm.solution;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.SdkTypeProxy;
And the code for creating a new custom entity record.
or could also be written as
// create the instace of DataContext class named xrm
var myXrm = new xrm("CRMOnPremise");
// use concept of Implicitly type local variable
// and object intializer
var testEntity = new new_test()
{
new_name = "Nishant",
new_lastname = "Rana"
};
myXrm.AddTonew_tests(testEntity);
myXrm.SaveChanges();
Similarly to update a record and delete a record we need to use
UpdateObject() and DeleteObject() function followed by call to
SaveChanges() method.
Bye.
Auto-Implemented Properties
Suppose this is our Person class with two properties First Name and Last Name.
}
}
Here we are not putting any extra logic while using get and set blocks for the properties, so the above code could be written as following using Auto-Implemented Properties.
}
Things to remember :-
Both get and set accessor are required.
To make it read only we can declare set accessor as private
Object Initializers
To initialize our Person object normally we would be writing the following code
However now we could do this in much simpler manner
Using Object initializers we can assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.
Extension Methods
Using Extension methods we can add methods to existing types without creating a new derived type, recompiling or modifying the original type.
Suppose this is how we have defined our Person class with one method named GetFirstName.
public string GetFirstName()
{
return FirstName;
}
}
Now we will add method to this existing Person Type named GetLastName
Here we need to follow these rules
Now the code that calls both these methods. GetLastName function would be called as if it was an instance method and not an static one.
Collection Initializers
Here instead of writing the following code to intialize the collection
Implicitly type local variables
Now we can declare variables using var keyword (implicitly) without explicitly defining their type.
Anonymous Types
Anonymous types are class types that contain one or more public read only properties.
var v = new { FirstName = “Nishant”, LastName = “Rana” };
The above line of code creates a anonymous class having two public read only properties named FirstName and LastName.
Things to remember :-
Anonymous methods and Lambda expressions
Suppose this is our simple delegate
And this is the function which it points to
And this is how we would be using the delegate
Now here instead of defining the MySimpleFunction separately we can make use of anonymous methods
Infact we can also use lambda expression over here
We can declare an optional parameter by specifying default value as a part of declaration
We can call the above method in any of the following way
Named Arguements
Dynamic Type
New dynamic type that allows us to do things that are resolved during run time.
dynamic dString = GetName(“a”,”b”);
dynamic dInt = 12;
Bye.
Check out this informative article !