SELECT DISTINCT on a DataTable then Sort them with ORDER BY

So I needed to get a list of items from a database. I really only wanted to get the information once then use it since this is a web application. I have always felt the less trips to the database the better. I get all the information in one query then decided to use LINQ to pull the information from the datatable that I needed to build each piece. The first step is to pull out the first layer. This needed to be sorted in a particular order.

This is for a book so each book would have a unit or section:

Then to bind just this information to the repeater in the sorted order:

There are probably better and more efficient ways to accomplish this, but this seemed to be the simplest.


UPDATE 6-4-2015:

I realized after looking at this code again it works, but my understanding of it is totally wrong. LINQ is used through out the code, I am just using two different types of syntax. The first section of code is using query syntax and the later is using my more preferred method of Lamda syntax. Both are using LINQ and are delayed. It just goes to show you that you are always learning and always looking at code you wrote years ago thinking, “what was I doing?”. I wrote this almost three years ago and was just learning LINQ, pretty sad my understanding of how LINQ really worked back then and what I have learned since.



Leave a Reply

Your email address will not be published. Required fields are marked *

Enter Captcha Here : *

Reload Image