How can I use Linq to select distinct rows from a Datatable and iterate. Var distinctRows = (from DataRow dRow in dTable.Rows select new.
Introduction Back in the days I came across a situation where I had to retrieve some data into a dataset table, then from that table I had to filter some records according to the user entry. For the selected record I had to find the history from the same table and port the details into Microsoft Excel and format it for the end user. I am sure most of you who will read this will say, yeah right big deal, just write a Stored Procedure and get the work done. Well, a Stored Procedure can only be written if you have access to the database. I was given select only on three tables and that was it, no more or less access to the DB. I did this class sometime back and have found that I use this file over and over in many projects that I have done, though not too fancy but may save someone's time on redesigning the wheel. I understand that my inline comments are not up to par and I do understand if demerit.
The reason for doing this class is to avoid the round trips to the server and letting the server use its resources for important work. Background You may simply import this file into your solution and start using it as it is. I must confess that I borrowed the idea from an article posted on Microsoft website and modified it to some complexity.
Speaking of complexity I am sure I must have bored you to almost death by now so without further due lets get into business. Using the code This class, on initialization takes a parameter, the parameter indicates the table on which the distinct method will apply, you may use one of the six overloaded functions to do the distinct selection on the table supplied during initialization. All the functions will return a datarow array. Let dissection the code: Initializing the class: Private mSourceTable As DataTable ' Initiate the distinct selector class ' The table on which to perform the distinct selection Public Sub New( ByVal MyTable As DataTable) mSourceTable = MyTable End Sub There is a local private variable that stores the table on which the distinct is to be applied. Next is selecting distinct rows, the rows could be selected distinct either on all the fields in the table, on selected rows (equivalent to where clause in SQL statement) or on select rows and (equivalent to where clause in SQL statement) and defined fields (columns).
For each of the three different distinct methods you could also apply the row state filters as well e.g. You may only apply distinct on all rows that have city as Suva and row stated as added.
The code below show the content of the file. I started as a part-time tutor at the University of the South Pacific, then I work as a programmer using MSAccess 97-2003, VB 6.0 VB.NET 1.0, VB.NET 1.1, MSSQL, MySQL, Oracle and Firebird for three years of which I have spent two years as a I.T. Manager for a manufacturing industry. Due to increasing demand in integrated software solutions specific to Fiji, I joined a software consulting company, so far I have develped a payroll, time and attendance, HRIS, municipality, accounting and POS software. I provide software and administration consulting for a number companies in Fiji. I am also interested in overseas contracts for Windows/Linux based design and developments. I currently hold BSc in computing Sci and Physics and MCP.
When answering a question please:. Read the question carefully. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem.
Insults are not welcome. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.