Skip to main content

Posts

Showing posts from February, 2014

LINQ: Overview of LINQ and it's advantages and disadvantages

Overview of LINQ: LINQ stands for Language Integrated Query, which is descriptive for where it's used and what it does. LINQ is used for querying data. Here I used the generic term "data" and didn't specified type of data. That's because LINQ can be used to query many different types of data, including SQL, XML, and even objects. It is a Microsoft programming model and methodology that gives formal query capabilities into Microsoft .NET-based programming languages (mainly in C# and VB.Net). LINQ Syntax: LINQ queries can be written through standard query expression or through Lambda expressions. Query expression syntax: var items = from item in Items where item.Price > 10 select item; Lambda expression syntax: var items = Items.Where(c => c.Price > 10).Select(c => c); Type of LINQ: Various type of LINQ available is: ·          LINQ to SQL ·      ...

C#: System.ArgumentException '.', hexadecimal value 0x00, is an invalid character.

Yesterday my client reported one issue she was facing on production server while generating excel file. Exception read as: “System.ArgumentException '.', hexadecimal value 0x00, is an invalid character” After further investigation I was able to replicate and exception is thrown due to one string with troublesome characters (i.e. control and non-UTF-8 characters). Solution: I have created one function (RemoveInvalidCharacters) to remove all the control and non-UTF-8 characters, and successfully removed all the troublesome characters. /// <summary> /// Method to remove control characters and other non-UTF-8 characters /// </summary> /// <param name="inputStr"> The string to process </param> /// <returns> String with no non-UTF-8 characters </returns> public static string RemoveInvalidCharacters( string inputStr) {     if (inputStr == null ) return null ;     var ...

LINQ: Grouping Data in LINQ using Group By

In this post I’ll explain how one can group data using LINQ GroupBy. GroupBy can be applied on different data types like Generic List, XML, DataTable etc. Syntax of Group By: var result= from c in <collection>     group c by c.<property> into g     select new     {         Key=g.Key,         Value=g     }; LINQ Grouping return partitioned sets of data from a collection based on a given key value, i.e. group employees by designation as: var query = from c in employees         group c by c.Designation; //or same statement can be written in Lambda expression as: var query = employees.GroupBy(c => c.Designation); GroupBy Result: Let’s understand result returned by LINQ grouping operation. The return collection fro...

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

While querying LINQ to Entities with check of Date, one may face the error like 'Date' is not supported in LINQ to Entities…… For example consider following LINQ statement, in which we are trying to get all employees created before today’s date: context.Employees.Where(c => c.CreatedOn >= DateTime .Now.Date ); If you compile this statement, it compiles successfully, however you'll get the error (mentioned below) when this query get executed. “The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported" Reason: Entity Framework doesn't allow the use of the Date member of DateTime inside an LINQ query. And because of the use of .Date member it throws the exception as when query get executed DateTime.Date cannot be converted to SQL statement. Solution: As I mentioned above LINQ to Entities queries are translated to SQ...