baros
baros

Reputation: 251

linq to sql, begin with query

I've a Languages table:

LangId LangName
1       English
2       EngTest
3       Germany

I want to write a query that shows LangName begins 'Eng'

var query = dc.Languages.Where(p=>p.LangName.Contains(txtBxLangNameFilter.Text));

I'm newbie in linq to sql. Can somebody show me how to write?

Upvotes: 4

Views: 8341

Answers (5)

Sanu Uthaiah Bollera
Sanu Uthaiah Bollera

Reputation: 937

var Lang= from language in  dc.Languages
            where language.LangName.StartsWith("Eng")
            select  language.LangName;

Upvotes: 0

Sean
Sean

Reputation: 442

Using the StartsWith() method here would be your best bet.

Upvotes: 0

Xaruth
Xaruth

Reputation: 4114

Use StartsWith instead of Contains

var query = dc.Languages.Where(p=>p.LangName.StartsWith(txtBxLangNameFilter.Text));

Upvotes: 0

Piotr Zierhoffer
Piotr Zierhoffer

Reputation: 5151

You'd want to change Contains to StartsWith.

Upvotes: 1

John Woo
John Woo

Reputation: 263833

Contains test if a string is found in a string at any location. Since you want to test for a string that starts with a certain string, use StartsWith().

var query = dc.Languages
              .Where(p => p.LangName.StartsWith(txtBxLangNameFilter.Text));

Upvotes: 13

Related Questions