Thursday, September 16, 2010

Data LINQ examples

To use LINQ (Language Integrated Query) you just need to have the following 2 namespace reference
using System.Collections.Generic;
using System.Linq;


// using where clasue within the collection.
List allCities = Common.GetAllCities().Where(c => c.DewPoint> 1).ToList() ;

// using where clasue & some property contain value search.
List allCities1 = Common.GetAllCities().Where(c => c.StationName.Contains("a")).ToList() ;

// pick the default value collection
Climate climate = Common.GetAllCities().SingleOrDefault();

// pick the First/default value collection
Climate climate1 = Common.GetAllCities().FirstOrDefault();

// sort the list
Common.GetAllCities().Sort();

Read xml data using Xml.Linq to generic list

using Xml.Linq.XDocument is very easy. now lets see how to pick the following xml file into xdocument and query with in the filedata using XML.Linq

< ?xml version="1.0" encoding="utf-8" ? >
< Cities >
< City StationName="Mumbai" >
< WeatherCondition >Not Extreem< /WeatherCondition >
< WindDirection >North-to-South< /WindDirection >
< Temperature >30 degree C< /Temperature >
< DewPoint >16< /DewPoint >
< Humidity >14.05< /Humidity >
< WindSpeed >Mild< /WindSpeed >
< /City >
< City StationName="Kolkata" >
< WeatherCondition >Dry< /WeatherCondition >
< WindDirection >East-to-South< /WindDirection >
< Temperature >32 degree C< /Temperature >
< DewPoint >11< /DewPoint >
< Humidity >12.35< /Humidity >
< WindSpeed >Strong< /WindSpeed >
< /City >
< City StationName="Delhi" >
< WeatherCondition >Hot< /WeatherCondition >
< WindDirection >East-to-North< /WindDirection >
< Temperature >42 degree C< /Temperature >
< DewPoint >7< /DewPoint >
< Humidity >11.35< /Humidity >
< WindSpeed >Strong< /WindSpeed >
< /City >
< /Cities >

Now read the above xml data using Xml.Linq.XDocument

public static List GetAllCities()
{
// Load a Xml File into XDocument class
XDocument xmlDoc = XDocument.Load(@"App_Data/CitiesClimate.xml");

// Read the Xml data into a
var cities = from xmlData in xmlDoc.Elements("Cities").Elements("City")
select xmlData;

Climate climate = null;
List climateList = new List();

//Now loop through all the cities in resultset
foreach (var city in cities)
{
climate = new Climate();
climate.StationName = city.Attribute("StationName").Value;
climate.WeatherCondition = city.Element("WeatherCondition").Value;
climate.WindDirection = city.Element("WindDirection").Value;
climate.Temperature = city.Element("Temperature").Value;
climate.DewPoint = Convert.ToDecimal(city.Element("DewPoint").Value) ;
climate.Humidity =Convert.ToDecimal(city.Element("Humidity").Value);
climate.WindSpeed = city.Element("WindSpeed").Value;
climateList.Add(climate);
}

return climateList;
}



Now see how to search some records
// Here we can perform search
var cities1 = from xmlData in xmlDoc.Elements("Cities").Elements("City")
where Convert.ToInt32(xmlData.Elements("DewPoint")) > 11
select xmlData;

Monday, September 6, 2010

FirstCoder, a utility for C# developer

Introdcuing an Utility that can reduce your work load specially if you are working in asp.net C#, Just connect your database ..and a few clicks all your business objects are ready to be used.

I have seen at least 40% of our time we spend in doing few standard functions for all the application we develop in our lifetime, sometimes I feel tired doing same monotonous task, i don’t believe in doing any job which can be automated, let the utility do all task for you….just relax!

I am sure many of you might have experienced the same.

Here I have identified few tasks for the same reason, and automated for all the following task for you.

Java Script Objects: The utility writes java script for all your business objects with few standard methods in it, like addToList(), removeFromList(), getById(id), getAll(),

JSON: The utility writes Json structure for all your business objects with few standard methods in it. Like Save, Get. I keep the JSON structure ready, you just need to set the value from your page control and you are done! JSON can make call to web service or WCF service.

WCF / Web Service: The utility creates wcf service for your entire object with a hosting plan on IIS and one page for service registration.

HBM: The utility gets your entire basic HBM ready within in a minute; I take care of id, composite key, one to many, one to one etc.

Business Entity: Write all business entity with custom attribute like wcf compatible, n-hibernate compatible.

Data Objects Structure: You must have seen often we change our database object structure during the development, but never update our technical specification, so here I create a document for all tables, views & procedures.

Once you standardize the definition, I produce exactly the same, you don’t have to spend time in object creation & to review them, and thus I also save some time, so more quickly you can concentrate on core business area.

Here is the link to download the utility

Upcoming Features :
1. Format all projects code at one click.
2. Identify all areas where function definition has not been written
3. Generate a report all of all function details, so we can identify if any duplicate function written or naming convention are incorrect, that may help to save the review time.

Wednesday, September 30, 2009

.Net 3.5 Framework & LINQ

When we talk about .net 3.5 framework, LINQ is a major thing, In simple word linq is a orm.

1. LINQ to Object
Queries performed against the in-memory data
2. LINQ to ADO.Net [Example]
2.1 LINQ to SQL (formerly DLinq) {Queries performed against the relation database only Microsoft SQL Server Supported}
2.2 LINQ to DataSet {Supports queries by using ADO.NET data sets and data tables}
2.3 LINQ to Entities {Microsoft ORM solution}

3. LINQ to XML (formerly XLinq)
{ Queries performed against the XML source}

In my site i have put all examples of linq and its use.

http://etgconsultancy.com/DotNetFrameWork/Framework3p5/LINQ-Implementation.aspx

A Complete implementation of LINQ to SQL can be found here :

http://etgconsultancy.com/DotNetFrameWork/Framework3p5/Linq-to-Sql.aspx

Sunday, September 6, 2009

Implement google map, how to get coordinates

Here are the very simple steps to implement google map in your application..

I assume you have a gmail account, and you have created your mark on http://maps.google.com

So first make your google location.

Now generate your key for google map, if you dont have click on following url to generate key http://code.google.com/apis/maps/signup.html

once you get your key, replace the key in following script.

< script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=yourkey" type="text/javascript" >< / script >
< script type="text/javascript" >

function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("divMap"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
map.setUIToDefault();
}
}

< / script >

Now place a div where you want to display the google map.

< div id="divMap" style="margin-top:12px;width:500px;height:400px;" >
< / div >

finally initialise the key

< script language="javascript" type="text/javascript" >
window.load= initialize();
< / script >


Google ma getting dipalyed on your site, but you might be wondering why your location not gettting displayed. For that you need to change the coorodinates.

How to get the coordinates ? Simply open your location in google map and copy paste the following javascript code on browser. youe get your location, replace the code in map.setCenter(new GLatLng(37.4419, -122.1419), 13); method in above script.


javascript:void(prompt('',gApplication.getMap().getCenter()));

Or

click on the right top "send" you get the following code

Hi, I'd like to share a Google Maps link with you.
Link: < http://maps.google.com/maps/ms?ie=UTF8&hl=en&msa=0&msid=103425934348104227970.000472c4205e6efea558c&ll=19.298314,72.861425&spn=0.007281,0.009624&z=17 >

Enjoy google map in your application.

Arindam
http://etgconsultancy.com