/// <summary> /// Searches for a <c>Customer</c> by name. /// <remarks> /// This method runs in multiple different modes depending on the input: /// /// If a single term is provided, then it will be searched in both the first and last /// names of customers. /// /// If two terms are provided and delimited with a space, the search will ensure that /// the customer's full name includes both search terms. /// /// If two terms are provided and delimited with a comma, the first term will be /// searched only in the last names of customers, and the second term will be /// searched only in the first names of customers. /// </remarks> /// </summary> /// <param name="ctx">Store context object.</param> /// <param name="name">Search for customers that have this search /// string in either their first or last name.</param> /// <returns>An <c>IQueryable</c> representing customers that have /// either a first or last name containing the search string.</returns> public static IQueryable <Customer> FindCustomerByName(this StoreContext ctx, string name) { name = name.ToLower(); // Search by either first name or last name when a space is present between two // search terms. Both search terms must be present in either the first or last // name (or both) of the customer in order to be considered a match. var nameComponents = name.Split(' ', 2); if (nameComponents.Length == 2) { var query1 = ctx.FindCustomerByName(nameComponents[0].Trim()); var query2 = ctx.FindCustomerByName(nameComponents[1].Trim()); return(query1.Intersect(query2)); } // Search for names in a "lastName,firstName" fashion when a comma is present // in the search query. var lastThenFirst = name.Split(',', 2); if (lastThenFirst.Length == 2) { var query1 = ctx.FindCustomerByLastName(lastThenFirst[0].Trim()); var query2 = ctx.FindCustomerByFirstName(lastThenFirst[1].Trim()); return(query1.Intersect(query2)); } // Standard single term search checks both first and last names. return(from customer in ctx.Customers where customer.FirstName.ToLower().Contains(name) || customer.LastName.ToLower().Contains(name) select customer); }
public void SearchesForCustomerByLastName() { var options = TestUtil.GetMemDbOptions("SearchesForCustomerByLastName"); using (var db = new StoreContext(options)) { var customer1 = new Customer("" + Guid.NewGuid().ToString()); customer1.LastName = "z--bfS/qc" + Guid.NewGuid().ToString(); db.Add(customer1); var customer2 = new Customer("" + Guid.NewGuid().ToString()); customer2.LastName = "1XfcnY" + Guid.NewGuid().ToString(); db.Add(customer2); var customer3 = new Customer("" + Guid.NewGuid().ToString()); customer3.LastName = "z--zB8tkm" + Guid.NewGuid().ToString(); db.Add(customer3); db.SaveChanges(); var findCustomer1 = db.FindCustomerByLastName("bfs"); Assert.Equal(1, findCustomer1.Count()); Assert.Equal(customer1.LastName, findCustomer1.First().LastName); var findCustomer2 = db.FindCustomerByLastName("cn"); Assert.Equal(1, findCustomer2.Count()); Assert.Equal(customer2.LastName, findCustomer2.First().LastName); var findCustomers = db.FindCustomerByLastName("z--"); Assert.Equal(2, findCustomers.Count()); Assert.Equal(customer1.LastName, findCustomers .Where(c => c.LastName == customer1.LastName) .First() .LastName); Assert.Equal(customer3.LastName, findCustomers .Where(c => c.LastName == customer3.LastName) .First() .LastName); } }