views:

27

answers:

1
public IQueryable<ArticleDisplay> SearchNumberOfArticles(int articleNr, string order)
        var result = (
                     from category in db.ArticleCategories
                     join article in db.Articles on category.CategoryID equals article.CategoryID                        
                     orderby article.Date order
                     select new ArticleDisplay
                     {
                         CategoryID = category.CategoryID,
                         CategoryTitle = category.Title,

                         ArticleID = article.ArticleID,
                         ArticleTitle = article.Title,
                         ArticleDate = article.Date,
                         ArticleContent = article.Content
                     }
                     ).Take(articleNr);

In PHP this would work, but in C# it doesn't. So, how to load keyword from variable and "print" it inside query? Here is order suppose to be replaced with descending or ascending. Thanks

+1  A: 

You can use an if statement

IQueryable<ArticleDisplay> SearchNumberOfArticles(int articleNr, string order)
{
    ................
    var articles = from category in db.ArticleCategories
                   join article in db.Articles 
                   on category.CategoryID equals article.CategoryID
                   select article;

    if (string.IsNullOrEmpty(order) || order == "ascending" || order = "asc")
    {
        articles = articles.OrderBy(a => a.Date).Take(articleNr);
    }
    else
    {
        articles = articles.OrderByDescending(a => a.Date).Take(articleNr);
    }
    .............
}
SteadyEddi
this is not working if I limit query with .Take(). i forgot to add it to my query (edited now)
ile
Did you put the take where I have put it? (edited)
SteadyEddi
Ok, this one is working. Thanks!
ile