views:

44

answers:

2
var query = from d in testPersons.person
            where d.Name == txtbox
            select d;

txtbox is text box on View page.

How change this code to return all names who starts with any part of name?

For example: When I type R in text box, on View page, this query should return all names who is start with character R

+4  A: 

You can use String.StartsWith.

var query = from d in testPersons.person
            where d.Name.StartsWith(txtbox)
            select d;

As long as Name is of type String you can do all string operations such as:

  • Contains
  • EndsWith
  • Equals
  • And many more..

Here are some more examples and information about LINQ and Strings.

Filip Ekberg
A: 

I have taken a slightly different interpretation of:

How change this code to return all names who starts with any part of name?

If you wished to retrieve a person whos first, middle or last names began with the letter typed in txtbox you could use:

var query = from d in testPersons.person
            where d.Name.StartsWith(txtbox) || d.Name.Contains(" " + txtbox)
            select d;
Nicholas Murray