LINQ-to-SQL 从变量加载关键字

发布于 2024-08-27 12:45:07 字数 978 浏览 4 评论 0原文

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

在 PHP 中这可以工作,但在 C# 中则不行。那么,如何从变量加载关键字并在查询中“打印”它?这里的 order 假设替换为 descendingascending。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如何视而不见 2024-09-03 12:45:07

您可以使用 if 语句

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

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);
    }
    .............
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文