Wesley Skeen
Wesley Skeen

Reputation: 8285

Query about Linq to Sql ordering

I am trying to figure out a better way of ordering the results of my sql query.

So far I have

List<ArticlePost> articlePosts = new List<ArticlePost>();
        IQueryable<ArticlePost> query = from ArticlePost in ArticlePostTable select ArticlePost;
        foreach (ArticlePost ArticlePost in query)
            articlePosts.Add(ArticlePost);
        articlePosts = articlePosts.OrderByDescending(ArticlePost => ArticlePost.atclID).ToList();

Is this a good way of sorting my list using linq or is there any way i can improve on it?

Upvotes: 1

Views: 56

Answers (3)

zmbq
zmbq

Reputation: 39023

What's wrong with the following?

var articlePosts = ArticlePostTable.OrderByDescending(ap => ap.actlId).ToList();

Upvotes: 5

Kundan Singh Chouhan
Kundan Singh Chouhan

Reputation: 14292

Try this instead :

List<ArticlePost> articlePosts = 
    ArticlePostTable
    .OrderByDescending(ArticlePost => ArticlePost.atclID)
    .ToList();

Upvotes: 0

Steven
Steven

Reputation: 172716

var articlePosts = (
    from post in ArticlePostTable
    orderby post.atclID descending
    select post)
    .ToList();

Upvotes: 2

Related Questions