【问题标题】:Building a multiple/single condition search function in NET.Core (IQueryable or IEnumerable)在 NET.Core 中构建多/单条件搜索功能(IQueryable 或 IEnumerable)
【发布时间】:2021-02-28 13:33:12
【问题描述】:

我是 C# 编码的初学者(实际上是一般的编码),我正在尝试创建一个网站来同时搜索 SQL Server 数据库中的多个和/或单个条件。到目前为止,它对于单个条件来说就像一个魅力,而且速度非常快,但到目前为止我还无法呈现多个条件搜索。我到处寻找可能的解决方案,但空手而归,要么是因为我还不知道如何实现它,要么是因为有些方法似乎不再起作用,包括多个 where 子句 && 和 | | where 子句和一些 Microsoft 教程中的运算符。

这是我的控制器:

var Projektdokumenter = from p in _context.ProjekterDokutypeDokuundertype
    .Include(p => p.Dokumenttype)
    .Include(p => p.Dokuundertype)
    .Include(p => p.Forfatter)
    .Include(p => p.P)
    .Include(p => p.Sprog)
    select p;    
    
    if (!String.IsNullOrEmpty(searchString) || (searchProject) != null) 
                {
                    if (!String.IsNullOrEmpty(searchString))
                    {
                        return View(Projektdokumenter.Where(p => p.Forfatter.Initialer.Contains(searchString)).ToList());
                    }
                    {
                        if (searchProject.HasValue)
                        {
                            return View(Projektdokumenter.Where(p => p.P.ProjektId.Equals(searchProject)).ToList());
                        }
                    }
                }  

这是我的观点

    <form asp-action="Index" method="get">
        <div>
            <p>
                Forfatter Initialer: <input type="text" name="searchString" value"" />
                Projekt ID: <input type="number" name="searchProject" value"" />
                <input type="submit" value="Search" class="btn btn-primary" />
                <a asp-action="Index">Remove Filter</a>
            </p>
        </div>
    </form>

【问题讨论】:

  • 您是说希望用户输入“Foo && Bar”并让 EFCore 将其解释为 AND 查询?

标签: asp.net-core asp.net-core-mvc ienumerable iqueryable


【解决方案1】:
  1. 创建一个模型来保存数据。将其命名为 SearchResult 或其他名称。

  2. 在表单中,您可以有多个搜索字符串,您可以将它们组合起来发送给方法。

  3. 创建一个 IQueryable

     IQueryable<ProjekterDokutypeDokuundertype> result = 
           _context.ProjekterDokutypeDokuundertype
                   .Include(p => p.Dokumenttype)
                   .AsNoTracking();
    
     if (!string.IsNullOrWhitespace(dokumentNavn))
     {
         result = result.Where(d => d.DokumentNavn == dokumentNavn);
     }
    
     if (!string.IsNullOrWhitespace(forfatterNavn))
     {
         result = result.Where(d => d.Forfatter == forfatterNavn);
     }
    
     var searchResult = await result.Select(s => new SearchResult { Forfatter = s.Forfatter, DokumentNavn = s.DokumentNavn}).ToListAsync();
    

【讨论】:

  • 明天我会试试你的解决方案,Johan 看看两者之间的区别。感谢您的输入
【解决方案2】:

你可以像下面这样改变你的控制器(假设你的searchProject是int类型,我建议你改变你的发布方法):

  public IActionResult Index(string searchString,int searchProject)
    {
        var Projektdokumenter = from p in _context.ProjekterDokutypeDokuundertype
        .Include(p => p.Dokumenttype)
        .Include(p => p.Dokuundertype)
        .Include(p => p.Forfatter)
        .Include(p => p.P)
        .Include(p => p.Sprog)
        select p;

        if (!String.IsNullOrEmpty(searchString) ^ searchProject!= 0)
        {
            if (!String.IsNullOrEmpty(searchString))
            {
                var c = Projektdokumenter.Where(p => p.Forfatter.Initialer.Contains(searchString)).ToList();
                return View(c);
            }
            {
                if (searchProject!=0)
                {
                    var d = Projektdokumenter.Where(p => p.P.ProjektId.Equals(searchProject)).ToList();
                    return View(d);
                }
            }
        }
        var e = Projektdokumenter.Where(p => p.Forfatter.Initialer.Contains(searchString) && p.P.ProjektId.Equals(searchProject)).ToList();
        return View(e);
    }

【讨论】:

  • 谢谢银秋。尽管我现在正在努力返回没有过滤器的视图,但这绝对是对我的查询的一个非常值得赞赏的答案。
猜你喜欢
  • 2012-03-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-23
  • 1970-01-01
  • 2010-11-06
  • 1970-01-01
  • 2019-09-16
  • 2017-03-31
相关资源
最近更新 更多