Skip to main content

Posts

Showing posts from December, 2009

DataTable to Dictionary using LINQ

Sometimes we have to create generic dictionary from the values in datatable. Usually we will loop through all the rows in the datatable and add the relevant keys to dictionary object for  ( int  _iExRowCnt = 0; _iExRowCnt < dsReturn.Tables[0].Rows.Count; _iExRowCnt++) { //add some code to chek null values & other validations if any _dictObj.Add(dsReturn.Tables[0].Rows[iExRowCnt][0].dsReturn.Tables[0].Rows[iExRowCnt][1]); } After LINQ was introduced there better way of doing the same addition. In the following code i am creating a datatable and populaing few dummy records. DataTable  dtTable =  new   DataTable (); dtTable.Columns.Add( new   DataColumn ( "ColumnNo" , typeof (System. String ))); dtTable.Columns.Add( new   DataColumn ( "controlType" ,  typeof (System. String ))); dtTable.Columns.Add( new   DataColumn ( "showVal" ,  typeof (System. Boolean ))); DataRow  dr; for  ( int  i = 0; i < 10; i++) { dr = dtTable.NewRow(); dr[0]

Anonymous types for dummies

Anonymous types are used to define strong types without defining the type. Anonymous types are strongly typed and checked at compile time. It provides a convenient way to encapsulate a set of read-only properties into a single object without having to first explicitly define a type. Anonymous types are reference types that derive directly from object. The compiler gives them a name although your application cannot access it. From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object. This type is widely used by LINQ, because LINQ returns dynamically-shaped data, whose type is determined by the LINQ query To understand better look at the following code static void Main ( string [] args)         {                 string [] names = { "Abhi1" , "abhi" , "abhi11" ,                        "george" , "bush" , "b

LINQ for dummies - an overview

Why do we need LINQ? Most of us would have wrote code to access data from different data sources a database, in memory objects , XML files or from other formats. We have different guidelines, architectures and methods to process and retrieve these data collection. For a data control in form it is immaterial whether the data is from XML or any other data sources. We have many relational OO databases but there always the gap between the data and its processing in Objects in any modern languages. Is LINQ the Holy Grail? I can’t decide on that. But LINQ tries to fill the vacuum between the datasources and their successful interpretation in Objects. With LINQ, Microsoft’s intention was to provide a solution for the problem of object-relational mapping, as well as to simplify the interaction between objects and data sources. LINQ eventually evolved into a general-purpose language-integrated querying toolset. This toolset can be used to access data coming from in-memory objects (LINQ t

Difference between a constant and readonly variables/fields : (constant vs readonly)

Most of us would have encountered these questions What is the difference between constant and readonly fields? When to use constant and when to use readonly fields? What is the advantage/disadvantage of each? Constants In C# you can declare a constant like this " const" is a keyword. public const string _constStrVar = "I am a static const str val"; A constant variable should have value at design time.  All the constant variables are static ie they are shared across all the instances of the class. You dont have to add the keyword "static". Constants are copied to all the dlls where is refereeing the parent class ie even if you change the the original constant value and recompile the parent dll , the other dll will still use the old value. The size of the executable goes up since all these constants are copied to the respective dlls Read Only  Read only variables are created usually in the constructor of class. there fore it will have valu

Return top N rows from datatable

Normally to return top N rows we use an SQL statement similar to the one below Select top N * from table but How can we achieve the same thing in DataTable I have seen many examples, using different methods. Most of the methods centered around the idea of creating new columns with automatic values increment and using them as index  There is better method using LINQ