Dynamic Linq

There are cases where you might need to use dynmaic linq in your Linq Queries, for instance filtering of items based on multiple where clause.

Assume you have list of following Items :

var listofmobiles = new List<Mobile>()


new Mobile(){ModelNo=1,MobileType= "Android"}

new Mobile(){ModelNo=2,MobileType= "Android"}

new Mobile(){ModelNo=3,MobileType= "BlackBerry"}

new Mobile(){ModelNo=4,MobileType= "Symbian"}

new Mobile(){ModelNo=5,MobileType= "IOS"



public class Mobile


public long ModelNo {get;set;}

public string MobileType {get;set;}


We would write a simple linq query if we where suppose to query only one of mobiletype,assume a case where there can be more than one mobiletype(Android,Ios) the list should still return the items under Android and Ios.

Expression Trees :

To write Dynamic Linq queries,we must construct expression tress that matches the where clause.I am not going to explain more about expression tree but instead usage of System.Dynamic.Linq feature provided by framework which uses expression trees.

Using Dynamic Linq in where clause :

Given different mobiletype (Ios,Android,Symbain ) filter out the above list that matches the mobiletype.

var filterresult =listofmobiles.where("mobiletye @0 or mobiletye @1 or mobiletye @2","Android","IOS","Symbain").Tolist();

#Things you may want to know about Constants and Readonly

constants (const): The value of const is set at compile time(the value must be set when you declare it) and cannot  be changed at run time.

ReadOnly : They need not be initialized  when declared ,but must be initialized in constructor. They could be initialized else where other than constructor.

  1. const are themselves static, so you cannot declare
     public static const int s =1 --Error cannot be static

2 .const can be used only with primitive types(exception is Strings) .i.e it does not make sense to use custom class with const.

 public cosnt myclass = new myclass() ---Error must be a compile time value 


Its possible for you to do that with ReadOnly.


#63 Polymorphism-C#

Polymorphism: Takes Many Forms.


1.Compile Time-(Early Binding/Overloading):Methods can have same name,with different number of arguments.

2.Run Time-(Late Binding/dynamic):Objects of Base Class can Point to objects of Derived class At Runtime.(using Override)

#60 Lock-C#

Lock is a thread safe mechanism,where you allow only one thread to access a  block of code(Critical Section) and block other threads,untill the current thread releases the lock(Mutual Exclusion).

lock (object)
 // crtical section

Internally the Lock get’s translated with Monitor.Enter(Object) and Monitor.Exit(object). Lock is a wrapper/high level construct provided by C#.

#57 Synchronization Constructs-Thread

Thread Synchronization Constructs can be performed in two ways 

1.User Mode Constructs-They utilize Hardware(Cpu Instruction to make thread safe),no interruption of OS.(Volatile,InterLocked,SpinLock).


2.Kernel Mode Constructs -Operating System takes care of thread synchronizations,and the os provide certain mechanism to handle thread safe block.(WaitHandle,Semaphore,Mutex)

A block  operation by thread in User Mode  is called LiveLock(Usage of CPU Forever). A block operation by thread in Kernel mode is called DeadLock(Wastage of Memory) 

#56 Critical Sections-Threads

Critical Section: Critical Section is a piece of code or a shared resource that  must not be accessed concurrently by different threads.

Problems(If CS is not handled):

1.Race Condition.


Mechanism to handle Critical Section:

1.Atomic Updates


3.Wait-Based Synchronization Protocol.