简体   繁体   中英

Searching using the last 4 digits

Just needed a tip for additional search functionality. I can search using the full 9 digit ItemID (123456789) but unable to add searching capability using the last 4 digits of ItemID on top of full digit search? ItemID does not hold fixed number. It should pick last 4 digit from any ItemID number.

  public ActionResult Search(SearchEmployeeModel searchEmployeeModel) 
   {
        searchEmployeeModel.ItemID = searchEmployeeModel.ItemID != null ? searchEmployeeModel.ItemID.Replace("'", "''") : searchEmployeeModel.ItemID;            
        searchEmployeeModel.First_Name = searchEmployeeModel.First_Name != null ? searchEmployeeModel.First_Name.Replace("'", "''") : searchEmployeeModel.First_Name;
                
    
List<SearchEmployeeModel> searchResult = repository.GetSearchItem(searchEmployeeModel.ItemID, searchEmployeeModel.First_Name);
    
   Session["SearchEmployeeModelHome"] = searchEmployeeModel;
   Session["searchResultHome"] = searchResult;
    
   return RedirectToAction("Index");
    
   }

public List<SearchEmployeeModel> GetSearchItem(string ItemID, string First_Name)
{
var searchItem = _db.Query<SearchEmployeeModel>("usp_SearchItem", new
{
@ItemID = ItemID
@First_Name = First_Name,    
}, null, true, 300, CommandType.StoredProcedure).ToList();

return searchItem;
}

It is unclear how you are getting the ids, but once you have the ids in a string list (or individual strings) one can use regex to determine if they are found by the last four digits in the string such as:

var ids = new List<string>() { "123456789", "678912345", "123456781", "666666789" };

var foundIds = ids.Where(id => Regex.IsMatch(id, "6789$")).ToList();

The pattern says look for 6789 at the end of the string $ . Once processed foundIds will contain two ids of "123456789" and "666666789".

string credit = "123456789"; ;
var last4 = credit.Substring(credit.Length - 4, 4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM