0

I am trying to perform a search using a single textbox for the search.

The idea is that the user can search the textbox either by Customer Name, Email, Phone Number, or Address.

I am analyzing the best possible way within my reach, i don't know if any of you have had the opportunity to work with something similar.

I do not know if using Regular Expressions I can identify the value entered by the user to know if I enter a phone number, address or email.

Thanks in advance, any example or contribution would be helpful.

This is my client class but my doubt is to identify the value of the text typed by the user.

public class Client
    {
        [Key]
        public int Id { get; set; }

        [StringLength(50)]
        public string FirstName { get; set; }

        [StringLength(50)]
        public string MidName { get; set; }

        [StringLength(50)]
        public string LastName { get; set; }

        [StringLength(50)]
        public string Email { get; set; }

        public virtual ICollection<Phone> Phones { get; set; }

        public virtual ICollection<Address> Address { get; set; }
    }

Thanks

Hector Lara
  • 182
  • 1
  • 10
  • I think, first of all, you should check if the value of text contains @ is email, number is phone. Something else you should search for name, address. – Tomato32 Aug 15 '19 at 04:17
  • You could find your answer [HERE](https://stackoverflow.com/questions/33153932/filter-search-using-multiple-fields-asp-net-mvc/33154580) – Matt Qafouri Aug 15 '19 at 05:40

2 Answers2

1

I would:

  1. Override ToString() in each class to return an amalgamation of the required searchable properties. See Here
  2. Use linq against all the things:

    string searchText;

    return clients.Where(x => x.ToString().Contains(searchText) || x.Address.Any(a => a.ToString().Contains(searchText) || x.Phones.Any(a => a.ToString().Contains(searchText));

You don't really need to complicate it by trying to work out what the types are to search. If it has an @ it will match on email, if it's a phone number of will match on that.

Jon Ryan
  • 1,497
  • 1
  • 13
  • 29
0

Hello I solved using the example of @Tomato32

  • It's only text = Full name
  • Only numbers = Phone
  • Only numbers and Length of 5 = Zip code
  • Text + numbers = Address

thanks

Hector Lara
  • 182
  • 1
  • 10