Dheyv
Dheyv

Reputation: 195

Match first character of a word in a String List with Linq

I have one LINQ query. I am just beginner in Linq. I wanted to use it for autocomplete. I need to match the input with starting character of a word in a string. Consider the scenario.

List of String

1. London Corporation
2. Financial Services Industry
3. European Union Countries.
4. Derivative Securities

In the above list, When the user presses character 'C' then the query should return the value 1 and 3 as matching character 'C' in a string of words. If the Input is 'S' then, the result should be 2 and 4.

I have tried the below one. But no idea about how can i proceed next. Please anyone tell me how can i split the word in a string and match for the input character. I tried the code below.

CODE

    var model = (from line in db.BibContents
                where line.TagNo == "020" && line.Sfld == "a" && line.Value.Split(' ').StartsWith(Val)
                    select new PoDetails
                    { 
                        BibId = line.BibId
                    }).ToList();

Thanks,

Upvotes: 2

Views: 2871

Answers (2)

Hosein
Hosein

Reputation: 581

The error you get is because there's no translation of c# Split() method into SQL Syntax.

In your case you can just check if the line either starts with <Val> or contains ' ' + <Val>:

var model = (from line in db.BibContents
             where line.TagNo == "020" && line.Sfld == "a" &&
             (line.IndexOf(Val) == 0 || line.IndexOf(' ' + Val) != -1)
                select new PoDetails
                { 
                    BibId = line.BibId
                }).ToList();

Upvotes: 1

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174309

You want something like this:

line.Value.Split(' ').Any(x => x.StartsWith(Val))

Upvotes: 4

Related Questions