【问题标题】:What is the proper algorithm for using Table<TEntity> and LINQ to filter a search?使用 Table<TEntity> 和 LINQ 过滤搜索的正确算法是什么?
【发布时间】:2015-05-17 22:17:29
【问题描述】:

我将解释我的设置。我有一张像

这样的桌子
    fileid    |                 orgid                      |              catid              |       fname
 ------------------------------------------------------------------------------------------------------------------
     1        |                   2                        |               1                 |      "mypowerpoint.ppt"
     2        |                   7                        |               12                |      "someworddoc.docx"
     3        |                   4                        |               8                 |      "homepageimg.jpg"
     4        |                   9                        |               4                 |      "some_text_document.txt"

还有两个相关的表格

    catid   |        orgid               |  catname   
 -------------------------------------------------
     1      |        1                   |  "Some Category"
     2      |        1                   |  "Some other Category"
     3      |        2                   |  "Category XYZ"
     4      |        3                   |  "My category" 

    orgid   |     orgname   
 ---------------------------------- 
     1      |     Company A
     2      |     Company B
     3      |     Company C
     4      |     Company D 

有了这些在后台,如果用户愿意,他们应该过滤掉他们对文件名 (fname) 的搜索。搜索检查他们放置的搜索是否包含在任何文件名中。他们可以按组织和/或类别过滤搜索。这些过滤器均以下拉列表的形式出现,如果不使用过滤器,则选择“全部”。他们的搜索结果作为 HTML 表格的行返回。

这是我返回这些搜索结果的方法:

    public ActionResult Search (string selectedOrgName, string selectedCatName, string searchVal)
    {
        PortalData PD = new PortalData();

        string htmlRows = "";

        foreach (AssetFile f in PD.files)
        {
            if (f.filename.Contains(searchVal))
            {
                string fOrgName = PD.orgs.FirstOrDefault(o => o.orgid == f.orgid).orgname;
                string fCatName = PD.cats.FirstOrDefault(c => c.catid == f.catid).catname;
                if (    (selectedOrgName == "ALL" || fOrgName == selectedOrgName)
                     && (selectedCatName == "ALL" || fCatName == selectedCatName) )
                {
                    htmlRows += "<tr>" + "<td><input type=\"checkbox\"/></td><td>" + fOrgName + "</td><td>" + fCatName + "</td><td>" + f.filename + "</td></tr>";  
                }

            }
        }

        if (string.IsNullOrEmpty(htmlRows)) htmlRows = "No results found!"; 

        return Content(htmlRows, "text/html");
    }

问题在于,如果我将来要添加新的过滤器,它看起来非常丑陋,可能效率低下,而且绝对不可扩展。我想知道这样做的正确方法是什么。

【问题讨论】:

  • 这里更丑陋的部分是您将 html 结果构建为字符串并返回 - 这也是不可扩展的部分。修复它并使用 MVC 框架自带的工具将数据传递给视图并在视图中渲染数据....然后担心数据的查询
  • 我是 MVC 新手。能否告诉我具体应该寻找哪个工具?
  • 创建一个新类,只包含在 html 中可以看到的属性,然后将视图绑定到这个新对象。然后在视图中对模型执行 foreach 循环并为每个项目显示一行

标签: c# sql asp.net-mvc linq razor


【解决方案1】:

我会先解决我的评论,但对于查询,请尝试以下内容:

IQueryable<myTable> PortalData;

// here is your first search / filter
PortalData = PortalData.Where(x => x.filename.contains(q));

// here is later future filters
// just an example of another filter on top of first one
PortalData = PortalData.Where(x => x.Id < whatever)

return PortalData.Select(x =>  new { x.orgName , x.catName });

【讨论】:

    猜你喜欢
    • 2015-03-06
    • 2013-05-24
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2019-12-15
    • 1970-01-01
    相关资源
    最近更新 更多