Thursday, December 27, 2012

Consider explicitly specifying the type of the range variable


from x in list

Compilation Error:

Could not find an implementation of the query pattern for source type 'System.Collections.IEnumerable'.  'Select' not found.  Consider explicitly specifying the type of the range variable 'x'.


Add the desired data type in front of the range variable x like

from Employee x in list

Wednesday, December 26, 2012

Are you missing a reference or a using directive for 'System.Linq'?

If you see the following error, it could be easy to fix it.


Could not find an implementation of the query pattern for source type 'System.Collections.Generic.List<string>'.  'Select' not found.  Are you missing a reference or a using directive for 'System.Linq'?


Just ensure you have done the following:

  • Reference to System.Core
  • using/Imports to System.Linq

Saturday, December 15, 2012

Returning Plain text from WCF service

Usually WebGet/Web Post will return data as JSON or Xml


In some cases, we may have to return a plain text or our own formatted text.


To do this, you can use the following steps,


1. Change the return type of the Method to Message instead of string

2. Return the text using by invoking WebOperationContext.Current.CreateTextResponse method

Thursday, December 13, 2012

Anonymous types

C# 3.5 supports anonymous types, through which you can declare simple objects with properties without Class definition

var v = new { Name = "Bob", Age = 20 };
Here a new type will be created with two properties Name and Age.
Name will have type String and Age will have type Integer.
var helps here to declare v using automatic type inferencing.
.Net will create a type and will use it implicitly.

Exception: Nullable object must have a value.

Exception: If you receive this error, it means you are trying to get a value from nullable object which has no values assigned.

Fix: To fix this you can check HasValue property before accessing the value

Tuesday, December 04, 2012

Type Inference 2

Automatic type inference will work only within code blocks such as constructors, methods, or property get/set blocks.


If you try to use type inference with var, the compiler will give the following error


The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

Sunday, December 02, 2012

Reflection: Assembly.GetExecutingAssembly

Consider the following call sequence.



When we execute the following code

Console.WriteLine("Executing :" + Assembly.GetExecutingAssembly().GetName().Name.ToString());
Console.WriteLine("Calling :" + Assembly.GetCallingAssembly().GetName().Name.ToString());
Console.WriteLine("Entry :" + Assembly.GetEntryAssembly().GetName().Name.ToString());

Output will be

Executing: OneMoreLib

Calling: AnotherLib

Entry: MyApp

Saturday, December 01, 2012

Reflection- Introduction

Reflection provides the way to access assembly, type and member information.

Even using reflection, we can create objects, read and write property values and invoke methods.

Reflection related classes can be found in System.Reflection Namespace, mscorlib assembly.

Reflection- Assembly

Through System.Reflection.Assembly, we can access assembly related information and also we can load an assembly into memory.
Following methods helps to get the information about the assembly.


Following methods helps to load an assembly into memory