Come from my prior technology which is Java, C# always make me “WOW” with its cool features that are not available in Java such as LINQ, Object Initializer and the one i am going to talk about, Anonymous Type.
Even, Indonesian java champion Frans Thamura stated that C# as language is cooler than Java and he said that unfortunately C# is running on top of CLR not JVM. Lol!.
Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first (Class). This is not so common because we always require to write Class to encapsulate properties to hold values.
Example of anonymous type is given below
1 2 3 4 |
var book=new { Title="How To Win Friends And Influence People", Author="Dale Carnegie" }; // Title : How To Win Friends And Influence People, Author : Dale Carnegie Console.WriteLine("Title : {0}, Author : {1}",book.Title,book.Author); |
Above, you can see that book is used to encapsulate both Title and Auhtor and instead it is instantiated as a certain type it goes anonymous.
Some of you may get this question in mind,
“Okay, this is cool. But why should i use anonymous type instead of class type when it is more comfortable and common usage?”
Based on my experience the answer is “because anonymous type gives you option to select property that you are interested in, others just let it go”.
Consider following example. You have a class named Student to store information about student in your application.
1 2 3 4 5 6 7 8 |
public class Student { public int StudentId { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Address { get; set; } public string Major { get; set; } } |
Then somewhere in your code you want to populate students data and show it, let’s say windows form app. You will do something like this in the constructor of the form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public FormStudent() { InitializeComponent(); List<student> students = new List<student>() { new Student(){ StudentId=1,FirstName="Adol",LastName="Hiller",Major="Politic",Address="NA"}, new Student(){ StudentId=2,FirstName="Bachelor",LastName="Martabak",Major="Economic",Address="NB"}, new Student(){ StudentId=3,FirstName="Charliy",LastName="Glossy",Major="Politic",Address="NC"} }; //Using EF // var students=Context.Students.ToList(); dataGridViewStudent.DataSource = students; } </student></student> |
Will have result like this
Actually you only interested in StudentId, FirstName and LastName property to be shown in the form, how to achieve it then?
Here anonymous type come to the rescue. Do some change below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public FormStudent() { InitializeComponent(); List<student> students = new List<student>() { new Student(){ StudentId=1,FirstName="Adol",LastName="Hiller",Major="Politic",Address="NA"}, new Student(){ StudentId=2,FirstName="Bachelor",LastName="Martabak",Major="Economic",Address="NB"}, new Student(){ StudentId=3,FirstName="Charliy",LastName="Glossy",Major="Politic",Address="NC"} }; //Using EF // var students=Context.Students.ToList(); var studentQuery = students.Select(s => new {StudentID=s.StudentId,Firstname=s.FirstName,Lastname=s.LastName }).ToList(); dataGridViewStudent.DataSource = studentQuery; } </student></student> |
As you can see anonymous type help us to select only property that we are interested in.Hope my little experience will help you and thank you for coming, spread the knowledge 😉
Gung…aku pernah baca ada tentang LINQ to XML. XML to XML, XML to classss..Itu apa toh gung?
Linq to Object, to SQL, to XML dll itu cuma data sourcenya aja yang berbeda
to Object berarti sumber datanya dari object, biasanya List atau IQueryable bisa juga IEnumerable
to SQL berarti sumber datanya SQL Server, kalau sekarang sih lebih baik pakai Entity Framework.