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

Add Web service reference- Components required to enumerate web references not installed

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

After playing with Web services for so many years this was a tricky error which kept me thinking for few minutes.
Today when i tried to add WCF reference to my application it gave the following error,
Error

—————————
Microsoft Visual Studio
—————————
Failed to update Service Reference ‘XXXBusiness.Reference’.
Error:The components required to enumerate Web references are not installed on this computer. Please re-install Visual Studio.(0×80004002)

What could have caused this error? Then i remembered there was a alert box from VS few hours ago which informed that some package was not loaded successfully and whether i wanted  VS to stop loading that package in future.Without reading the full message I accidentally pressed “YES” I know mea cupla.
But i have to move forward …

Now the solution
  1. Close all open Visual Studio instances
  2. Open the Command prompt available with Visual Studio and type devenv /resetskippkgs (You can directly type this to Run command or normal command prompt)
  3. Add reference to the Web service/WCF service

Now I am back in business :)

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

Follow

Get every new post delivered to your Inbox.