Here is my attempt, I can not guarantee that this will work. I need to think more about how to get a dynamic column name, I'm not sure about that. EDIT: you can use row for order column.
int typeId = 1115; bool orderAscending = false; string columnName = "StringColumn"; var query = from ent in SomeEntities join ea in Attributes on ea.EntityId = ent.Id where ea.AttributeTypeId = typeId; if(orderAscending) { query = query.OrderBy(ea => columnName).Select(ea => ea.Value); } else { query = query.OrderByDescending(ea => columnName).Select(ea => ea.Value); }
var results = query.ToList (); // call toList or list to execute the query because LINQ has deferred execution.
EDIT: I think the ordering after the selection stops when ordering. I moved the select statement after ordering. I also added "query =", but I'm not sure if this is necessary. At the moment, I have no way to verify this.
EDIT 3: I released LINQPad today and made a few tweaks to what I had before. I modeled your data in a code-based approach to using EF, and it should be close to what you have. This approach works better if you are just trying to get a list of attributes (which you are not). To get around this, I added the Entity property to the MyAttribute class. This code works in LINQPAD.
void Main() {
source share