开发者

How can I sort the results of a LINQ query

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.


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( ... );


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);


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

Something like:

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


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);


Here ya go:

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


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜