Skip to main content

Posts

Showing posts with the label Linq

Anonymous types - sorting & working with Linq

Consider the following code        var numbers = new int[] { 1, 4, 6, 2, 9, 10, 0, 99, 33 };             var sortedNums = from n in numbers                              orderby n descending                              select n;             foreach (var v in sortedNums)                 Console.WriteLine(v);             var sports = new string[] { "soccer", "baseball" };             var Name = from sport in sports select new { Title = sport };             foreach (var s in Name)                 Console.WriteLine(s.Title); Line 3 sorts the arr...

Anonymous types - Examples

Simple   var simple = "Simple Ann Type";     string str= "Simple Ann Type"; Both the code are identical for MSIL Array Initializer var name = new string[] { "a","b","c" }; Console.WriteLine(name[0]); Composite Anonymous Types We can think of this use of anonymous types as defining an inline class without all of the typing.     var Person = new {FirstName="Bill", LastName="Clinton"};           Console.WriteLine(Person);//will produce - {FirstName="Bill", LastName="Clinton"}           Console.WriteLine(Person.FirstName);//will produce - Bill           Console.WriteLine(Person.LastName);//will produce - Clinton           //Person.FirstName = "s"; - ERROR : Error 1 Property or indexer AnonymousType#1.FirstName' cannot be assigned to -- it is read only   Console.ReadLine(); A nice feature added to anon...

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++)...

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"...

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...

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