使用 C# 在 ASP.NET MVC 中使用多个参数过滤数据

Posted

技术标签:

【中文标题】使用 C# 在 ASP.NET MVC 中使用多个参数过滤数据【英文标题】:Using multiple parameters for filtering data in ASP.NET MVC using C# 【发布时间】:2022-01-13 18:30:55 【问题描述】:

我正在做一个图书馆项目,您应该能够按出版商、类别、星级、网站等过滤图书。

我想出了一个解决方案,但是它由多个 if 语句组成,如果我想在将来添加更多过滤器,我认为它的可扩展性不高。

这是我的BookController 中处理它的方法之一:

public IActionResult Index(int? id, string title, string forlag, int rating)

    //Search by title given by category id
    var list = (from ep in _db.Books
                join e in _db.BookCategories 
                on ep.BookID equals e.BookID
                where e.CategoryID == id && ep.Title.Contains($"title")
                select new Book
                
                    BookID = ep.BookID,
                    Title = ep.Title,
                    Author = ep.Author,
                    Isbn = ep.Isbn,
                    Publisher = ep.Publisher,
                    Sites = ep.Sites,
                    ReleaseDate = ep.ReleaseDate,
                    Summary = ep.Summary,
                    Picture = ep.Picture,
                    AddedDate = ep.AddedDate,
                    Stars = ep.Stars,
                ).ToList();

    var queryID = (from u in _db.Books
                    join e in _db.BookCategories
                    on u.BookID equals e.BookID
                    where e.CategoryID == id
                    select new Book
                    
                        BookID = u.BookID,
                        Title = u.Title,
                        Author = u.Author,
                        Isbn = u.Isbn,
                        Publisher = u.Publisher,
                        Sites = u.Sites,
                        ReleaseDate = u.ReleaseDate,
                        Summary = u.Summary,
                        Picture = u.Picture,
                        AddedDate = u.AddedDate,
                        Stars = (from c in _db.bookComments where c.BookID == u.BookID && c.IsApproved == true select c.Stars).Average(),
                    ).ToList();

    var queryID2 = (from u in _db.Books
                    select new Book
                    
                        BookID = u.BookID,
                        Title = u.Title,
                        Author = u.Author,
                        Isbn = u.Isbn,
                        Publisher = u.Publisher,
                        Sites = u.Sites,
                        ReleaseDate = u.ReleaseDate,
                        Summary = u.Summary,
                        Picture = u.Picture,
                        AddedDate = u.AddedDate,
                        Stars = (from c in _db.bookComments where c.BookID == u.BookID && c.IsApproved == true select c.Stars).Average(),
                    ).ToList();


    var sortedID = queryID.Where(o => o.Stars >= rating);
    var sortedID2 = queryID2.Where(o => o.Stars >= rating);


    //Search by title if category id equals 0
    var bookList = _db.Books.Where(o => o.Title.Contains($"title"));

    //Sort by category, publisher & star rating
    if (id > 0 && forlag != null && rating > 0)
    
        var newListus = sortedID.Where(o => o.Publisher == forlag);
        return View(newListus);
    

    //Sort by publisher & star rating
    if (id == 0 && forlag != null && rating > 0)
    
        var peter = sortedID2.Where(o => o.Publisher == forlag);
        return View(peter);
    

    //Sort by category & publisher
    if (id > 0 && forlag != null)
    
        var newList = list.Where(o => o.Publisher == forlag);
        return View(newList);
    

    //Sort by category & star rating
    if (id > 0 && rating > 0)
    
        return View(sortedID);
    

    //Sort by star rating
    if (id == 0 && rating > 0)
    
        return View(sortedID2);
    

    //Sort by publisher
    if (id == 0 && forlag != null)
    
        var bookListSorted = _db.Books.Where(o => o.Publisher == forlag);
        return View(bookListSorted);
    
    if (id == 0)
    
        return View(bookList);
    

    return View(list);

任何更具可扩展性和更简单的解决方案将不胜感激。如果需要更多信息,请告诉我。

马特奥

【问题讨论】:

这个查询可以简化。你能翻译你的cmets吗?我丹麦语不好,这里的人很多。谢谢。 嘿@SvyatoslavDanyliv cmets 现在应该翻译了!感谢您的关注! 【参考方案1】:

您可以使用 LINQ 的功能查询组合。

我已经定义了中间匿名类来处理书和它的星星。

public IActionResult Index(int? id, string title, string forlag, int rating)

    // filterout by title and set stars to default value
    var books = _db.Books
        .Where(b => b.Title.Contains(title))
        .Select(b => new  Book = b, Stars = 0.0 );

    var isStarsDefined  = false;

    if (id.HasValue)
    
        //Search by title given by category id and assign category's Starts
        books = 
            from b in books
            join c in _db.BookCategories on b.Book.BookID equals c.BookID
            where c.CategoryID == id
            select new  Book = b.Book, Stars = c.Starts ;

        isStarsDefined  = true;
    

    // additional filter by publisher
    if (forlag != null)
    
        books = books.Where(b => b.Book.Publisher == forlag);
    

    // well probably for result we need stars
    if (!isStarsDefined)
    
        books = books.Select(b  => new 
         
            Book = b.Book, 
            Stars = _db.bookComments
                .Where(c => c.BookID == b.Book.BookID && c.IsApproved == true)
                .Select(c => c.Stars)
                .Average()
        )
    

    // filter by rating
    if (rating > 0)
    
        books = books.Where(b => b.Stars >= rating);
    

    // make final projection to DTO
    var result = books
        .Select(b => new Book
        
            BookID = b.Book.BookID,
            Title = b.Book.Title,
            Author = b.Book.Author,
            Isbn = b.Book.Isbn,
            Publisher = b.Book.Publisher,
            Sites = b.Book.Sites,
            ReleaseDate = b.Book.ReleaseDate,
            Summary = b.Book.Summary,
            Picture = b.Book.Picture,
            AddedDate = b.Book.AddedDate,
            Stars = b.Stars
        )
        .ToList();

    result View(result);

请注意,我已经从内存中编写了所有内容,并且可能存在一些小的编译问题。此外,如果您在中间使用ToList,您将失败,一切都应该通过IQueryable 工作。

【讨论】:

嘿!这看起来很棒,但是“e.bookID”是什么。我收到此错误:错误 CS1938 名称“e”不在“等于”右侧的范围内。考虑交换 'equals' 两边的表达式。 已修复,我不是编译器 ;)

以上是关于使用 C# 在 ASP.NET MVC 中使用多个参数过滤数据的主要内容,如果未能解决你的问题,请参考以下文章

列表中的 ASP.NET MVC C# 列表

C# asp.net mvc 创建虚拟目录

如何使用 C# 在 ASP.NET Core 3.1 MVC 中使用会话变量

在 ASP.NET MVC 中使用 AJAX 方法时使用 C# 控制器重定向到页面

使用 ASP.net、C#、MVC 在模板中生成 pdf

C# 和 ASP.NET MVC:在视图中使用 #if 指令