0

I have a search function that searches for a specific word in two related tables and one unrelated table. Owner table and Registration table are related, and Vehicle table has no relations to the other two tables. I've tried multiple ways of approaching this problem, but non of them have worked. Below is the code in question.

What I have tried so far was placing three tables within a ViewModel and called it within my controllers search method.

Owner Table
public int OwnerId {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}

Registration Table
public int RegistrationId {get; set;}
public DateTime RegisteredDate {get; set;}
public string RegistrationLocation {get;set;}
public int OwnerId {get;set;}

Vehicle Table
public int VehicleId {get;set;}
public string VehicleName {get;set;}
public DateTime VehicleYear {get;set;}
public string Model {get;set;}

SearchViewModel

public IEnumerable<Owner> Owners {get;set;}
public IEnumerable<Registration> Registrations {get;set;}
public IEnumerable<Vehicle> Vehicles {get;set;}

Controller

[HttpGet]
 public SearchViewModel Search(string searchString)
{ 
   SearchViewModel searchVM = new SearchViewModel();
   searchVM.Owner = searchVM.Owner.Where(o => o.FirstName.Contains(searchString));
   searchVM.Registration = searchVM.Registration.Where(r => r.RegistrationLocation.Contains(searchString));
   searchVM.Vehicle = searchVM.Vehicle.Where(v => 
   v.Model.Contains(searchString));

   return searchVM;
}

Another way I've tried is:

[HttpGet]
 public IActionResult Search(string searchString)
{ 

 var owner = from owners in _context.Owner select owners; 
 var registration = from registrations in _context.Registration select 
 registrations; 
 var vehicle = from vehicles in _context.Vehicle select vehicles; 


   owner = owner.Where(o => o.FirstName.Contains(searchString));
   registration = registration.Where(r => 
   r.RegistrationLocation.Contains(searchString));
   vehicle = vehicle.Where(v => 
   v.Model.Contains(searchString));

   return Ok(); //I'm not sure how to return the three.
}
7
  • What isn't working? Are you ever actually querying the database? It looks like you're querying the view model itself, but where does it get its data? Commented Feb 5, 2019 at 19:07
  • I don't see a method which retrieves the data from db Commented Feb 5, 2019 at 19:08
  • Hi David, that is probably where I'm going wrong. I need to query the database. How do I go about doing that? Commented Feb 5, 2019 at 19:08
  • 1
    @ChrisH.: If you currently have no code which queries a database at all then where you'd start is basically a tutorial on using a database in ASP.NET MVC/WebAPI. Entity Framework is usually the framework used for that purpose. Commented Feb 5, 2019 at 19:09
  • I was thinking of this method => var owner = from owners in _context.Owner select owners; var registration = from registrations in _context.Registration select registrations; var vehicle = from vehicles in _context.Vehicle select vehicles; Commented Feb 5, 2019 at 19:15

1 Answer 1

1

Combine your two attempts. In the first attempt you're building a view model, but not querying the database. In the second attempt you're querying the database, but not building a view model. Do both.

For example:

var searchVM = new SearchViewModel();

searchVM.Owners = _context.Owner.Where(o => o.FirstName.Contains(searchString));
searchVM.Registrations = _context.Registration.Where(r =>  r.RegistrationLocation.Contains(searchString));
searchVM.Vehicles = _context.Vehicle.Where(v => v.Model.Contains(searchString));

return searchVM;
Sign up to request clarification or add additional context in comments.

3 Comments

you're a genius. Thank you so much. Another thing I've noticed is that when I'm searching for a specific word, it only retrieves results that match the data in the database. How can I search for results that are not case sensitive? For example, I want to be able to search for "car" & "Car". Right now, I'm only able to retrieve results based on "Car"
@ChrisH.: There may be a more elegant way to do it, but I usually just append .ToLower() to the relevant strings so they're all lowercase. For example: .Where(o => o.FirstName.ToLower().Contains(searchString.ToLower()))
You've done it again @David. Thank you again for your help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.