/// <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 SearchesForCustomerByFirstName() { var options = TestUtil.GetMemDbOptions("SearchesForCustomerByFirstName"); using (var db = new StoreContext(options)) { var customer1 = new Customer("--z6HkRe" + Guid.NewGuid().ToString()); db.Add(customer1); var customer2 = new Customer("dPmth" + Guid.NewGuid().ToString()); db.Add(customer2); var customer3 = new Customer("--za/tvV" + Guid.NewGuid().ToString()); db.Add(customer3); db.SaveChanges(); var findCustomer1 = db.FindCustomerByFirstName("6HkRe"); Assert.Equal(1, findCustomer1.Count()); Assert.Equal(customer1.FirstName, findCustomer1.First().FirstName); var findCustomer2 = db.FindCustomerByFirstName("Pmth"); Assert.Equal(1, findCustomer2.Count()); Assert.Equal(customer2.FirstName, findCustomer2.First().FirstName); var findCustomers = db.FindCustomerByFirstName("--z"); Assert.Equal(2, findCustomers.Count()); Assert.Equal(customer1.FirstName, findCustomers .Where(c => c.FirstName == customer1.FirstName) .First() .FirstName); Assert.Equal(customer3.FirstName, findCustomers .Where(c => c.FirstName == customer3.FirstName) .First() .FirstName); } }