Share via


how to do a search in name and surname (concatenate both)

Question

Thursday, October 26, 2017 9:42 AM

Hi, i have a search form where i can search for my user

today i have done this

 var saloncollaborators = db.SalonCollaborators as IQueryable<SalonCollaborator>;

            //SEARCH TITLE
            if (!String.IsNullOrEmpty(searchString))
            {
                saloncollaborators = saloncollaborators.Where(r => r.UserForCollaborator.FirstName.ToLower().Contains(searchString.ToLower()) || r.UserForCollaborator.LastName.ToLower().Contains(searchString.ToLower()));
            }

but the problem is when someone try to find a whole name like Jhonny Deep and it does not work

because FirstName or LastName does not contains Jhonny Deep, but only 

FirstName : Jhonny 

and 

LastName : Deep

how can i fix this search to be able to find a whole name?

All replies (20)

Monday, October 30, 2017 11:12 AM âś…Answered

Hi grafic.web,

if a user try to search by NAME+USERNAME but he can even search for USERNAME +NAME

No matter how we connect two strings, it will has its order. If we use the code you give above, it can still only search by one of 'NAME+USERNAME' and 'USERNAME +NAME'. I think you can ask the user to enter a blank between each type of name. Such as 'Name UserName' or 'User Name Name'. Then the controller can be like this.

var users = db.Users as IQueryable<ApplicationUser>;
var AllUsers = new List<Users>();
if (!String.IsNullOrEmpty(searchString))
{
    string[] names = searchString.Split(' ');
    foreach(var name in names)
    {
        users = users.Where(r => r.FirstName.ToLower().Contains(name) | r.NickName.ToLower().Contains(name) | r.UserName.ToLower().Contains(name)).ToList();
        AllUsers.AddRange(users);
    }
}

Best Regards,

Daisy


Thursday, October 26, 2017 12:06 PM

Try with this:

(r.UserForCollaborator.FirstName.ToLower() + " " + r.UserForCollaborator.LastName.ToLower()).Contains(searchString.ToLower())

Thursday, October 26, 2017 1:16 PM

 split them by space and use the first the first name the second for the second.


Friday, October 27, 2017 12:56 PM

Hi kipo,

thanks for you answer!!

This code does not work, do you have another solution?


Friday, October 27, 2017 1:02 PM

In what way it doesn't work? How exactly did you use the code I posted?


Friday, October 27, 2017 1:13 PM

Hi ,

i done this :

users = users.Where(r => (r.FirstName.ToLower() + " " + r.LastName.ToLower() + " " + r.NickName.ToLower() + " " + r.UserName.ToLower()).Contains(searchString.ToLower()));

and for example i have a user called :

Name : **cathy **

Surname : marcon

If i try to seach only the word cathy , i can see the user

but if i search the whole cathy  marcon

no users are displyed in my list!

Online i saw something like this :

SELECT FirstName + ' ' + LastName As FullName FROM Customers

but i do not know how to apl to my code..

my code is like this :

 var users = db.Users as IQueryable<ApplicationUser>;

//SEARCH
            if (!String.IsNullOrEmpty(searchString))
            {


                users = users.Where(r => (r.FirstName.ToLower() + " " + r.LastName.ToLower() + " " + r.NickName.ToLower() + " " + r.UserName.ToLower()).Contains(searchString.ToLower()));
            }

//Pagination
            int pageSize = 100;
            int pageNumber = (page ?? 1);
            return View(users.ToPagedList(pageNumber, pageSize));

Friday, October 27, 2017 1:36 PM

var s = searchString.Split(' ');
var query = _db.Where(n => n.FirstName == s[0] && n.LastName == s[1]).ToList();

something like that, you can make it more complicate if that is emptry by search for just last names, just first name etc, you also have to validate there are two names or it will have an error if there is only 1.


Friday, October 27, 2017 1:44 PM

Hi EnenDaveyBoy ,

i do not want to be rude, but i asked you to delete your post because are out off topic..

if tou read the code, i also serach for a nickname and email, and more over, your solution is not useful because we will never know if one user want to write NAME SURNAME or SURNAME NAME...

So, please, avoid to write if you do not have a real code solution.

Thanks for your comprension


Friday, October 27, 2017 2:38 PM

your title is "how to do a search in name and surname"

why doesn't my code do that?

if you also want to search other ways you can add them to the where statement, but thats not the title or main part of your question, and i thought you would be able to do it yourself

if you want it deleting you will have to report it.


Friday, October 27, 2017 2:43 PM

var s = searchString.Split(' ');
If(s.Count() > 1)
{
var query = _db.Where(n => (n.FirstName == s[0] && n.LastName == s[1])  || n.NickName == searchString || n.Username == searchString).ToList();
}
else
{
var query = _db.Where(n => n.FirstName == searchString || n.LastName == searchString  || n.NickName == searchString || n.Username == searchString).ToList();
}

Or something like that


Friday, October 27, 2017 2:49 PM

Hi EnenDaveyBoy,

thanks for your code!

But you solution is to diffiucult in fact the real problem is if a usertry to search by NAME+USERNAME but he can even serach for USERNAME +NAME so i cannot know if in my search i start with username or with name...

I saw there is a way to concatenate them.. but i do not know how to do..

i am sure there is a simple way to create only one value containing NAME+ " " + USERNAME and search inside this value..

but i do not know how to do..


Friday, October 27, 2017 3:05 PM

the solution is more complex than you think. you are better of buying a solution (see address standardization libraries).

but if you want to code it, here is the common approach

when inserting a record for each name field
     stem the name (usually some sort of soundex)
     insert the name and record key into a stem/key table

create a table of alias names to standard (it common to use the stem versions) :
    jon => john
    fred to fredrick

this table varies by language / culture

when the user searches,

    break the search term in words
    standardize and stem the search words
    search the stem/key table and join back to the table with the names
    using and standardized compare algorithm, calc a confidence score
    sort the return only ro above your chosen confidence score

 

 

  


Friday, October 27, 2017 3:23 PM

@Bruce, are there free stem seeds? or/and are the any things like code projects tutorials to show an example? i's love to see how to build the confidence score and use that to search, something i will be needing in the furture.

(sorry for the hijack)


Friday, October 27, 2017 3:31 PM

Hi, saw ther is a kind of solution like this..

 public class MyClass
    {   [Required]
        public string userid { get; set; }
        [Required]
        public string username { get; set; }
        [Required]
        public string firstname { get; set; }
        [Required]
        public string lastname { get; set; }
        

        public string Fullname { get { return string.Format("{0} {1}", firstname, lastname); } }
    }

or

public class MyClass
    {
        public int PersonID  { get; set; }
        public string First { get; set; }
        public string Last { get; set; }

        public string NameFull
        {
            get {

                return First + " " + Last;
            }
        }
    }

but i really would love to d by db..

so, something like this :

var plist = from p in DB.personList select p.FirstName + ", " + p.LastName;
            return View(plist);

But, how to do in my controller?


Monday, October 30, 2017 2:51 PM

@Bruce, are there free stem seeds? or/and are the any things like code projects tutorials to show an example? i's love to see how to build the confidence score and use that to search, something i will be needing in the furture.

(sorry for the hijack)

look fuzzy match libraries. for names one these should work:

use soundex to match, and distance for confidence. 

if you want an alias table, it better to buy, they are large and culture dependent.

stemming is also culture dependent. search engines have open source code for this. 


Monday, October 30, 2017 3:23 PM

Hi,

Or if you still need help we would need first to understand the exact search you want :

- you want an exact match for LastName+" "+FirstName or FirstName+" "+LastName ?

  • another option would be to ensure that each word of your search string (splitted on white spaces) is found at least in one of your search fields

In the later case a search for "Robert Martin" would find also "Martin, Robert Junior" for example unlike the first case. It would be likely my default approach.


Monday, October 30, 2017 9:26 PM

@bruce, many thanks.


Tuesday, October 31, 2017 2:10 PM

Hi PatriceSc,

in fact, what i want is to be able to find inside my user table what User the costumer is looking for..

the porble is that Costumer can serach for an user filling the text box wiht NAME and LASTNAME 

but my in my users table i have them in 2 different place

UserFirstName

UserLastName

So i nee to search inside them.. and i was wandering how to do to be able to do so..


Tuesday, October 31, 2017 2:20 PM

Hi X.Daisy

why in your code to do de OR you do like this :

r.FirstName.ToLower().Contains(name) | r.NickName.ToLower().Contains(name)

and not like this?

r.FirstName.ToLower().Contains(name) || r.NickName.ToLower().Contains(name)

what the difference?


Wednesday, November 1, 2017 10:33 AM

Hi grafic.web,

The conditional-OR operator (||) performs a logical-OR of its bool operands. If the first operand evaluates to true, the second operand isn't evaluated. If the first operand evaluates to false, the second operator determines whether the OR expression as a whole evaluates to true or false.(Comes from here.)

I think using | or || here has no effect.

Best Regards,

Daisy