LINQ for dummies – an overview

December 26, 2009 at 12:35 pm | Posted in .Net, ASP.net, C#, VB.NET | Leave a comment
Tags: , , ,

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 to Objects), databases (LINQ to SQL), XML
documents (LINQ to XML), a file-system, or any other source.

LINQ can be used to access any type of object or datasource. The syntax remains the same. Previously we had to use different methods like ADO.Net.  XPath, IO packages etc to retrieve data ( ok still we can use these methods and in many cases I still prefer them over LINQ)

Broadly classifying we have three major categories of LINQ

  1. LINQ to Objects,
  2. LINQ to SQL,
  3. LINQ to XML

Don’t worry there are other categories like LINQ to datasets. LINQ to Entities ( with ADO.net entity framework). In Visual Studio you can write LINQ queries in Visual Basic or C# with SQL Server databases, XML documents,ADO.NET Datasets, and any collection of objects that supports IEnumerable or the generic IEnumerable(T) interface. In short .NET Language-Integrated Query defines a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language. Third parties are also free to replace the standard query operators with their own implementations that provide additional services such as remote evaluation, query translation, optimization, and so on. By adhering to the conventions of the LINQ pattern, such implementations enjoy the same language integration and tool support as the standard query operators.

Next – LINQ in action …

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

December 26, 2009 at 12:34 pm | Posted in .Net, ASP.net, C#, VB.NET | 1 Comment
Tags: ,
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 values before the constructor of the class exists
class MyClass
{
public readonly string _strReadonly;
public void MyClass(string strVal)
{            _strReadonly = strVal;
}
}
http://thetechjungle.blogspot.com/2009/12/difference-between-contstant-and.html

Return top N rows from datatable

December 26, 2009 at 12:32 pm | Posted in .Net, ASP.net, C#, VB.NET | Leave a comment
Tags: , , , , ,
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 
public  DataTable GetTopNFromDataTable(int TopRowCount, DataTable dtSource)
{

var dtTrec = from item in dtSource.AsEnumerable()
select item;
var topN = dtTrec.Take(TopRowCount);
DataTable dtNew = new DataTable();
dtNew = dtSource.Clone();

foreach (DataRow drrow in topN.ToArray())
{

dtNew.ImportRow(drrow);
}

return dtNew;
}

var dtTrec – stores the item in datatable, Using the Take function of Linq the first N rows is filtered
var topN = dtTrec.Take(TopRowCount);
Now how to retrieve the rows between N1 & N2, just use the skip function along with Take as shown below

public DataTable GetTopBetweenFromDataTable(int intFrom, int intTo, DataTable dtSource)
{
……
var topN = dtTrec.Skip(intFrom).Take(intFrom);

……

}
http://thetechjungle.blogspot.com/2009/12/return-top-n-rpws-from-datatable.html

Viewstate error : 12031

December 26, 2009 at 12:28 pm | Posted in .Net, ASP.net, C# | Leave a comment
Tags: , , , , ,

Few days ago I was asked to look into an issue. In our application we have created dynamic grids to show data from database. This ASPX page was Ajax enabled. Moreover all the rows of the grid were in edit mode ie. the normal controls like textbox,dropdowns etc  were displayed in all the rows. This grid was Paginated. But for the past few days the paging was not working.

I executed the page and found that the page was generating an error 12031  with the following message

Sys.webForms.PageRequestManagerServerErrorException:An unknown error occurred while processing the request on the server .the status code returned from the server was:12031.

On my first round of analysis I found the issue with Viewstate. If the viewstate is large then connection is reset (ERROR_INTERNET_CONNECTION_RESET ). In local machine with less load this problem will not occur but as the load & network latency increases this error will come. Once this error is generated the general events of grid is not triggered. So advised my team to minimize the use of viewstate. It will help in to load the page faster & reduce the network traffic. I can increase the maxRequestlength value to allow more data but ideally i shouldn’t increase that.


In the page tested by me a page with grid  with 372 rows generated a viewstate of 4.2 mb. you can disable viewstate using EnableViewState=”false” for the individual controls and for the entire page also.

With every post back this much of data is transferred back & fro & this will result in low response time.

The developer was saving all the data into viewstate in page load and from that the data was populated to the grid.

Better solution is to retrieve only the required data from database, minimize the use of  viewstate, Viewstate can be compressed also. About all these I will update in another post.

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.