0
var search = (from c in db.**
                          where c.first_name == txtbox.Text || Convert.ToInt32(c.id = txtbox.Text)
                          select new
                          {
                              ID = c.ID,
                              FullName = c.name,
                              FirstName = c.first,
                              LastName = c.last
                          });

I am trying to use a textbox to search database for the name if the name is entered or if they decide to use an ID then search by the ID. When I try it sees that a letter is being used and it's trying to find a number.

Also I would like it so say I enter "A" into the textbox I would like to search for everyone that has an "A" in their name.

Any help would be awesome thanks.

KratosMafia
  • 333
  • 1
  • 16

2 Answers2

1

Try something like this:

var searchText = txtbox.Text.Trim();
var searchForId = false;
int id;
if(int.TryParse(searchText, out id)){
    searchForId = true;
}

var search = (from c in db.**
    where 
        c.first_name.Contains(searchText) 
        || searchForId && c.id == id
    select new
    {
        ID = c.ID,
        FullName = c.name,
        FirstName = c.first,
        LastName = c.last
    });
cederlof
  • 7,206
  • 4
  • 45
  • 62
0

what you want to do is contains not equal found this i hope it helps How to do SQL Like % in Linq?.

Community
  • 1
  • 1
Eric
  • 122
  • 8