Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ -- easy "StartsWith" issue

Posted on 2013-12-17
3
Medium Priority
?
3,496 Views
Last Modified: 2016-02-10
Below #2 works when using the "MicrosoftProduct"
LIST, but fails when trying to get via the
"Lookup_PartNumbers" database table.

How can I get the below #2
working when using a database table ?

Steps
 1. user types "10"
 2. all PartNumbers that "startWith" 10.... from
    Lookup_PartNumbers should appear, but nothing does
 3. user types "100"
 4. all PartNumbers that are exact matches from
    Lookup_PartNumbers appear correctly
----------------------------------------------------------------------------

        public JsonResult getData(string term)
        {
            /*
            List<string> MicrosoftProduct = new List<string>();
            MicrosoftProduct.Add("Office");
            MicrosoftProduct.Add(".NET");
            MicrosoftProduct.Add("Visual Studio");
            MicrosoftProduct.Add("sql server");
            MicrosoftProduct.Add("Windows7");
            MicrosoftProduct.Add("Window8");
            */

            var mySearchResults =
                from p in db.Lookup_PartNumbers
                //.Where(c => c.PartNumber.StartsWith(term, StringComparison.CurrentCultureIgnoreCase))
                    //above display below error
                    //LINQ to Entities does not recognize the method 'Boolean StartsWith(System.String, System.StringComparison)' method, and this method cannot be translated into a store expression.
                       
                //.Where(c => c.PartNumber.Contains(term))
                    //above does not error, but typing "10" displays nothing
                    //only after typing the FULL partNumber, "100", does the result display

                //.Where(c => c.PartNumber.StartsWith(term))
                    //above does not error, but typing "10" displays nothing
                    //only after typing the FULL partNumber, "100", does the result display

                .Take(10)
                .Select(c => c.PartNumber + c.PartDescription)
                .ToList()
                select p;

            List<string> getValues = mySearchResults.Where(item => item.ToLower().StartsWith(term.ToLower())).ToList();

            // Return the result set as JSON
            return Json(getValues, JsonRequestBehavior.AllowGet);
        }
0
Comment
Question by:finance_teacher
  • 2
3 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 39724060
Because the Linq to SQL and Linq to Entity Framework work a little differently with strings please indicate which technology are you using.
0
 

Author Comment

by:finance_teacher
ID: 39724333
LINQ to Entities, but am willing to do anything to make it work.
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39724558
Hi finance_teacher;

Linq to Entity Framework only implements one of the overloads of the StartsWith methods the one that has only one parameter the other two are not implemented.

This should work.

var mySearchResults = db.Lookup_PartNumbers
                        .Where(c => c.PartNumber.StartsWith(term))
                        .Select(c => c.PartNumber + c.PartDescription)
                        .Take(10)
                        .ToList();

Open in new window


Now the above Linq to Entity Framework query should get translated to the following T-SQL statement

ELECT TOP (10) 
[Extent1].[PartNumber] + [Extent1].[PartDescription] AS [C1]
FROM [dbo].[Lookup_PartNumbers] AS [Extent1]
WHERE [Extent1].[PartNumber] LIKE N'10%'

Open in new window


Note that the server filters on LIKE N'10%' so if any PartNumber starts with "10" it should return it.

Pattern Matching by Using LIKE
LIKE supports ASCII pattern matching and Unicode pattern matching. When all arguments (match_expression, pattern, and escape_character, if present) are ASCII character data types, ASCII pattern matching is performed. If any one of the arguments are of Unicode data type, all arguments are converted to Unicode and Unicode pattern matching is performed. When you use Unicode data (nchar or nvarchar data types) with LIKE, trailing blanks are significant; however, for non-Unicode data, trailing blanks are not significant. Unicode LIKE is compatible with the ISO standard. ASCII LIKE is compatible with earlier versions of SQL Server.

Please see the following documentation for reason why it may not be returning anything.
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question