Skip to content

C/C++/C#

Using Select Method on a DataTable in C# Dot Net

In previous article I populated a DataTable called ‘leagueTable’ with data read in from a comma delimited text file and performed operations on this data to get it into the format I wanted.

The league table has the name of the team and number of wins and draws home and away ready to calculate the relative strengths of each result. For each home team I will re evaluate the rating of its opponent based on all its other away results excluding the current fixture.

For example the fixture Team A v Team B, would require re evaluating all away matches for Team B excluding the current one against Team A. To do this I need to run a query on the DataTable that holds all the result data called ‘importResultsTable’.

One feature of the DataTable is that it can perform SQL queries on the data it holds in memory by using its ‘Select’ method. The result from this select method will return a number of DataRows. In the example we want to re-evaluate the away opponent ‘Team B’ based on all its away results except against Team A.

In SQL query analyser we would select this data by using the following query

Select * from importResultsTable where home’TeamA’ and away=’Team B’

It is very similarly for the select method in DataTable only the ‘select’ part of the query is already done. I’m using a string variable called ‘AnExpression’ that I’m passing as a parameter into the select method of the DataTable. The query could be passed in directly if wanted.

string AnExpression = “awayTeam=” ‘ “+awayTeam+” ‘ and homeTeam’ “+TeamA+” ‘ “;

DataRow[] awayTeamFixtures = importResultsTable.Select(AnExpression);

From the resulting array of DataRows (awayTeamFixtures) we can re-evaluate the away team value. An example of returned data is as follows

Team C v Team B 1-0

Team D v Team B 2-0

Team E v Team B 1-1

By giving points for a win and draw we can give a value to Team B away performance. Having two losses and a draw Team B away from home is not such a tough opponent so any home win against them may not be worth as much as other home wins.

Article Source: http://EzineArticles.com/?expert=Antonio_Viatle

Be Sociable, Share!
    The following two tabs change content below.

    Add Your Comment (Get a Gravatar)