0

I have the following query. Is there an easy way that I could sort the results in numeric order of RowKey. Currently RowKey is a string. I would like to sort _rowData and NOT modify this query as I want to try some different sort orders and I'm looking for one line changes and not to redo the query.

var _rowData = (from qu in _que.GetAll(
  u => u.PartitionKey == partitionKey )
  select new {
    qu.PartitionKey,
    qu.RowKey,
    qu.Style,
    qu.ShortTitle,
    ModifiedToString = String.Format("{0:MM/dd/yyyy HH:mm}", qu.Modified)
});

I want to do the sorting outside of the LINQ query. The reason is that actually I want to be able to let the user choose a sort of RowKey, ModifiedToString and ShortTitle.

6 Answers 6

1

When you use the OrderBy extension, you don't modify the original query, it just enumerates the same values:

var _rowData = ...
var _rowDataSortedByRowKey = _rowData.OrderBy( u => u.RowKey );
var _rowDataSortedByModifiedToString = _rowData.OrderBy( u => u.ModifiedToString );
var _rowDataSortedByShortTitle = _rowData.OrderBy( u => u.ShortTitle );
var _rowDataSortedByOther = _rowData.OrderBy( ... );
Sign up to request clarification or add additional context in comments.

Comments

0

Try specifying the DataRowComparer.Default in the Distinct statement.

Example:

var query = (from c in functiongettable().AsEnumerable()
            orderby c.Field<string>("Name")
            select c).Distinc(DataRowComparer.Default);

1 Comment

DataRowComparer? Sorry but what is that? I am using RowKey which is a string representation of a numeric rowid.
0

You can use System.Linq.Enumerable.OrderByDescending()

Something like:

(from qu in _que.OrderByDescending(u => u.RowKey).GetAll(
  u => u.PartitionKey == partitionKey )

Comments

0

You can order the elements of a LINQ query using the orderby keyword:

OrderBy - Simple 2

This sample uses orderby to sort a list of words by length.

public void Linq29()
{
    string[] words = { "cherry", "apple", "blueberry" };

    var sortedWords =
        from w in words
        orderby w.Length
        select w;

    Console.WriteLine("The sorted list of words (by length):");
    foreach (var w in sortedWords)
    {
        Console.WriteLine(w);
    }
}

The sorted list of words (by length):

apple
cherry

You can also use the Enumerable.OrderBy Extension Method:

var sortedWords = words.OrderBy(w => w.Length);

If you want to order by different criteria, you can pass a different key selector for each criterion:

if (c == "Length")
    sortedWords = words.OrderBy(w => w.Length);
else
    sortedWords = words.OrderBy(w => w);

4 Comments

Sorry but I want to do this outside of the LINQ query. The reason is that actually I want to be able to let the user choose a sort of RowKey, ModifiedToString and ShortTitle. I was thinking this could be three different resort options after the above query.
@Robert Dawson: Then make a second query using the first query as input? var query = from x _rowData orderby x.RowKey select x;
thanks dtb. I think this is the solution i wanted. Just one question. How do I change the string so I can sort numerically. Currently row 9 comes after row 80.
@Robert Dawson: Parse the string to an integer using int.Parse: var query = from x _rowData orderby int.Parse(x.RowKey) select x;
0

Here ya go:

_rowData.Sort((a, b) => Convert.ToInt32(a.Key).CompareTo(Convert.ToInt32(b.Key)));

Comments

0

Try using the OrderBy extension method in the System.Linq namespace:

http://msdn.microsoft.com/en-us/library/bb549264.aspx

This method will allow you to add ordering to your query conditionally.

var _rowData = (from qu in _que.GetAll(
  u => u.PartitionKey == partitionKey )
  select new {
    qu.PartitionKey,
    qu.RowKey,
    qu.Style,
    qu.ShortTitle,
    ModifiedToString = String.Format("{0:MM/dd/yyyy HH:mm}", qu.Modified)
});

// Let's say the user has selected they want to sort by RowKey
// and ModifiedToString, but not ShortTitle.
bool sortByRowKey = true; 
bool sortByModifiedToString = true;
bool sortByShortTitle = false;

var query = _rowData.AsQueryable();

if(sortByRowKey) {
    query = query.OrderBy(x => x.RowKey);
}

if(sortByModifiedToString) {
    query = query.OrderBy(x => x.ModifiedToString);
}

if(sortByShortTitle) {
    query = query.OrderBy(x => x.ShortTitle);
}

// Now let's call ToList() to execute the query and get the ordered data.
var _orderedData = query.ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.