2

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,

Dheyv
  • 195
  • 4
  • 16

2 Answers2

4

You want something like this:

line.Value.Split(' ').Any(x => x.StartsWith(Val))
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Yes, But showing error like this: LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method, and this method cannot be translated into a store expression. – Dheyv Jan 07 '13 at 12:53
  • Try using `line.Value.ToString().Split(' ').Any(x => x.StartsWith(Val))` (sorry - this is difficult to debug without the code in front of me). Failing that, see http://stackoverflow.com/questions/8653200/split-string-with-linq – Aaron Newton Jan 08 '13 at 01:40
  • P.S. you can always debug your Fluent API chans by breaking them up like `var part1 = line.Value.Split('');` and then `var part2 = part1.Any(x => x.StartsWith(Val));` and using `F10` with a breakpoint to step between them. – Aaron Newton Jan 08 '13 at 01:54
  • @AaronNewton: That's not the problem here. The code is correct, but LINQ to Entities doesn't understand `string.Split`. – Daniel Hilgarth Jan 08 '13 at 08:58
  • I wasn't trying to provide the solution. It was a generic comment suggesting a technique which can (and probably would) assist the OP to locate the source of an error in a fluent API chain by splitting it into its components (also, like I said it's difficult to debug this without having the OPs code with VS running in front of me). Also, did you look at the link in my first comment? Specifically, look at this answer, which addresses the question at hand (and basically says what you just said) - http://stackoverflow.com/a/8653259/201648 – Aaron Newton Jan 08 '13 at 11:29
1

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();
Hosein
  • 581
  • 1
  • 7
  • 29
  • The query returns error as like, Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid') are supported in this context. – Dheyv Jan 08 '13 at 03:56