Search code examples
asp.net-coreasp.net-core-mvcienumerableiqueryable

Building a multiple/single condition search function in NET.Core (IQueryable or IEnumerable)


I am a beginner in coding in C# (and coding in general, really) and I am trying to create a website to search a SQL Server database for multiple and/or single conditions simultaneously. So far it works like a charm, and very fast, for single conditions but I have so far not been able to render the multiple conditional search. I have scoured both here and there for possible solutions but have come up empty handed either because I couldn't figure out how to implement it yet or because there are methods that doesn't seem to work anymore including multiple where clauses, && and || operators in where clauses and several Microsoft tutorials.

This is my controller:

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

And this is my view

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

Solution

  • You can change your Controller like below(Assuming your searchProject is of type int,and I suggest you to change your method to post):

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