Using Anonymous Types, Type Inference and Generics in LINQ


Server Intellect


Using Anonymous Types, Type Inference and Generics in LINQ

New in ASP.NET 3.5 is the ability to use Type Inference with C# and VB.NET. When using Type Inference, you are allowing the compiler to decide the data type of a variable at run time. This can be useful when using variables of which you don't know the content type. In order to use Type Inference in C#, we are required to use the new keyword 'var', just like in JavaScript:

C#:

var displayMessage = "This is a message.";

VB.NET:

Dim displayMessage = "This is a message.";

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

This code snippet will declare a new variable of type String. The compiler will guess the data type based upon the type of data assigned to it. Type Inference will only work if a value is assigned to it when it is first declared. For example, the following C# will not work; the following VB.NET will technically work, but not as intended:

C#:

var displayMessage;
displayMessage = "This is a message";

VB.NET:

Dim displayMessage;
displayMessage = "This is a message";

Yes, it is possible to find a good web host. Sometimes it takes a while. After trying several, we went with Server Intellect and have been very happy. They are the most professional, customer service friendly and technically knowledgeable host we've found so far.

Along with Type Inference, Anonymous Types are another addition to ASP.NET 3.5 and allow you to create a type without creating a whole class, which can be useful for the smaller objects. For example, An Anonymous Type may look like this:

C#:

var Person = new {FirstName = "Michael", LastName = "Hall", Age = 13};

VB.NET:

Dim Person = New With {.FirstName = "Michael", .LastName = "Hall", .Age = 13};

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

LINQ to SQL also makes use of Generics. Generic collections are very easy to initialize and can be done so like this:

C#:

List<String>Fruits = new List<String>();
Fruits.Add("Strawberry");
Fruits.Add("Orange");
Fruits.Add("Apples");

VB.NET:

Dim Fruits As New List(Of String)
Fruits.Add("Strawberry");
Fruits.Add("Orange");
Fruits.Add("Apples");

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

C# also allows us to declare a collection on one line, like so:

List<String>Fruits = new List<String> {"Strawberry", "Orange", "Apple"};

VB.NET does not support Collection or Array initializers.
In order for us to use these Generic Collections, we will need to import the following assembly reference:

C#:

using System.Collections.Generic;

VB.NET:

Imports System.Collections.Generic



Comments
www.greececasino.com said:

Well standard LINQ is a new addition to .NET that allows the programmer to query inline data as they probably would be used to doing with standard SQL-type syntax.basically what LINQ allows us to do. And as one can imagine, DLINQ does similar stuff but with database objects, and XLINQ does queries/creation over XML documents.

LINQ also introduces lot of concepts that have really come from other functional programming languages, such as Haskell, LISP.

Posted 12/17/2009 at 2:19 AM
web development said:

The iterative process continues until all the following conditions are valid for any given argument:

* The argument is a lambda expression (L), from which no inferences have been made.

* The corresponding parameter's type (P) is a delegate type, which has a return type that involves one or more method type parameters.

* Both L and P have the same number of parameters, and the same modifiers if they're explicit or no modifiers if L has an implicitly typed parameter list

Posted 03/07/2010 at 10:59 AM
web development said:

Please continue to post blogs like this to help people who are not so much of a techie, like me, to be motivated in big thanks for the useful info I found on Via Talk’s attempt to steal Vonage Customers.

Posted 03/09/2010 at 1:50 PM
leather sandals said:

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post

Posted 04/04/2010 at 7:22 PM
medical ppo said:

I'm writing a program in C# for my structured programming class. These are the directions:

Write a program for a college's admissions office. The user enters a numeric high school grade point average (for example, 3.2) and an admission test score. Print the message "Accept" if the student meets either of the following requirements:

>> A grade point average of 3.0 or higher and an admission test score of at least 60

>> A grade point average of less than 3.0 and an admission test score of at least 80

If the student does not meet either of the qualification criteria, print "Reject". Save the program as Admission.cs.

Okay, now here's my source code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ConsoleApplication3

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Please enter your high school cumulative GPA.");

single gPA = Console.ReadLine();

Console.WriteLine("Please enter your admission test score.");

int testScore = Console.ReadLine();

if (gPA >= 3.0 && testScore >= 60) || (gPA < 3.0 && testScore >= 80)

Console.WriteLine("Accept");

else

Console.WriteLine("Reject");

}

}

}

I'm using Microsoft Visual Studio, and it's giving me four error messages. "Invalid expression term '||' (highlighting the || in my "if" statement), "; expected" (highlighting the blank space after the parenthesis after the "80" in my "if" statement), "Invalid expression term 'else'" (highlighting the word "else" in my "else" expression), and "; expected" again (highlighting the blank space after the word "else").

What am I doing wrong?

Posted 04/25/2010 at 5:52 AM
Oppapers.com said:

As LINQ became most common in programming to query the data from various data sources it became a must for all computer student or programmers to study LINQ. And hence the LINQ tutorials became so important between us. This article is a tutorial about using anonymous types, type interfaces and generics in LINQ. In the first section the writer explains about how type interfaces are declared and initialized for C# and VB .NET separately. He also shows the wrong codes usually seem to us as true, to clear the confusions. The next section is about the usage of anonymous type. This also explained separately for C# and VB .NET. The last section of this article contains the details of initialization of generic collections. This article is very much useful for those who are looking to learn the basics of LINQ usage in ASP .NET 3.5.

Posted 05/04/2010 at 9:40 PM
fuel saving devices said:

Using Anonymous Types, Type Inference and Generics in LINQ - this is useful for us, the internet people you know :) Thanks!

Jessica Jameson - I am a Newbie Internet Marketer :)

Posted 05/05/2010 at 4:12 AM
sito di poker preferito said:

I am looking for new ways to write some prototypes taking advantage of C# 3.0 new features. Type inference is extending intellisense possibilities making code extremely fast to write. I am also trying to make the use of lambda expressions more familiar.

Posted 05/18/2010 at 2:48 AM
camellialan said:

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog?

Posted 05/24/2010 at 7:14 PM
sonnerie portable gratuite said:

I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.

Posted 05/24/2010 at 7:15 PM
bad credit car loan said:

It's a necessary feature invented for C# in Visual Studio .Net 2008 to work efficiently with LINQ query.

Posted 06/11/2010 at 6:39 PM
Free dating sites said:

Well done. I am going to blog about this too...

Posted 06/16/2010 at 12:56 PM
Vegas Slot Machines said:

That's a really neat solution when combined with the IQueryable change that John suggested. I have been putting up with having to hit the database for Linq to SQL queries to date and it was starting to really bug me! Tell your friend he needs to blog!!...

Posted 07/14/2010 at 7:39 AM
tranny cams said:

Nicely done mate, I have been looking for this kind of info on other blogs and none of them went into the details as you did.

Posted 07/30/2010 at 9:05 PM
search engine optimization los angeles said:

which you don't know the content type. In order to use Type Inference in C#, we are required to use the new keyword 'var',

Posted 08/06/2010 at 7:46 AM
junk yards said:

Converting an anonymous inner class into a top level class takes 2 main refactorings: Convert it to a nested class and then convert the nested class to a top level class. If you have a nested class (ie. a named inner class

Posted 08/09/2010 at 12:55 PM
placement argent said:

This is a nice blog. Good clean UI and nice informative blog. I will be coming back soon, Thanks for posting some great ideas and I'll try to return back with a completely different browser to check things out! Also, I put a link to your blog at my site, hope you don't mind?

Posted 08/10/2010 at 2:48 AM
cleaning services said:

Excellent help for me and all who reading.

Posted 08/17/2010 at 12:51 AM
free sign up bonus bingo uk said:

I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with yo

Posted 08/19/2010 at 8:46 PM
property maintenance said:

Excellent help. Over all I just loved the post. It was so much helpful.

Posted 08/20/2010 at 12:26 AM
Stock Picks said:

Great article, i just read one and you make simple conclusion and powerful to learn. thanx mate !

Posted 08/20/2010 at 5:28 AM
coach outlet said:

coach outlet

Posted 08/26/2010 at 1:57 PM
louis vuitton bags said:

louis vuitton bags

Posted 08/26/2010 at 1:58 PM
coach outlet store online said:
Posted 08/26/2010 at 9:18 PM
Arvind Kumar said:

I hear and I forget, I see and I remember. I do and I understand...

Hey!

So don't forget to visit - <a href ='http://www.faqpanel.com'> www.faqpanel.com </a> ,

See it and remember it, Do read FAQs on www.faqpanel.com

and understand how to conquer the interview.

Posted 08/28/2010 at 9:27 AM

Leave a Comment