简体   繁体   中英

Change orderby depending on value of var

I have a foreach with an ordebydescending(), but in one case I need to use an orderby() instead. Depending on the value of articleType how can I use an inline condition inside the foreach to allow this to happen.

This is the condition I need to build into to determine the use of orderbydescending or orderby

if (articleType == BusinessLogic.ArticleType.Webinar)

This is the full function

public static List<Article> GetArticles(BusinessLogic.ArticleType articleType, long languageID)
{
    List<Article> articles = new List<Article>();

    using (var db = new DatabaseConnection())
    {
            foreach (var record in db
                .GetArticles(BusinessLogic.SystemComponentID, (int) articleType, languageID)
                .OrderByDescending(c => c.Date_Time))
            {
                #region articleTextLanguageID

                long articleTextLanguageID = GetArticleTextLanguageID(record.ID, languageID);

                string previewImageName = GetArticle(record.ID, articleTextLanguageID).PreviewImageName;

                #endregion

                Article article = new Article()
                {
                    ID = record.ID,
                    Title = record.Title,
                    Summary = record.Summary,
                    PreviewImageName = previewImageName,
                    Date = record.Date_Time,
                    ArticleTextLanguageID = articleTextLanguageID
                };

                articles.Add(article);
            }
    }

    return articles;
}

Was thinking something along these lines, but its not working

foreach (var record in db
    .GetArticles(BusinessLogic.SystemComponentID, (int)articleType, languageID)
    .Where(articleType == BusinessLogic.ArticleType.Webinar.ToString()?.OrderByDescending(c => c.Date_Time)) : .OrderBy(c => c.Date_Time)))

The way to do this would be to construct the query in pieces. For example:

var query = db.GetArticles(BusinessLogic.SystemComponentID, (int)articleType, languageID);

if(articleType == BusinessLogic.ArticleType.Webinar)
{
   query = query.OrderByDescending(c => c.Date_Time);
}
else
{
   query = query.OrderBy(c => c.Date_Time);
} 

foreach(var record in query)
{
   // process 
}

If you required additional sorting, you'd need an extra variable typed as IOrderedEnumerable / IOrderedQueryable (depending on what GetArticles returns) as an intermediate to chain ThenBy / ThemByDescending :

var source = db.GetArticles(BusinessLogic.SystemComponentID, (int)articleType, languageID);

IOrderedEnumerable<Article> query;
if(articleType == BusinessLogic.ArticleType.Webinar)
{
   query = source.OrderByDescending(c => c.Date_Time);
}
else
{
   query = source.OrderBy(c => c.Date_Time);
} 

if(somethingElse) 
{
   query = query.ThenBy(c => c.OtherProperty);
} 

foreach(var record in query)
{
   // process 
}

Based on your comment below, as notes above the second example would look more like the following (this means that db.GetArticles returns an IQueryable<Article> and not an IEnumerable<Article> ):

var source = db.GetArticles(BusinessLogic.SystemComponentID, (int)articleType, languageID);

IOrderedQueryable<Article> query;
if(articleType == BusinessLogic.ArticleType.Webinar)
{
   query = source.OrderByDescending(c => c.Date_Time);
}
else
{
   query = source.OrderBy(c => c.Date_Time);
} 

if(somethingElse) 
   query = query.ThenBy(c => c.OtherProperty);

foreach(var record in query)
{
   // process 
}

You could also shorten it to the following:

var source = db.GetArticles(BusinessLogic.SystemComponentID, (int)articleType, languageID);

var query = articleType == BusinessLogic.ArticleType.Webinar
          ? source.OrderByDescending(c => c.Date_Time)
          : source.OrderBy(c => c.Date_Time);

if(somethingElse) 
   query = query.ThenBy(c => c.OtherProperty);

foreach(var record in query)
{
   // process 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM