Solved

Need help with WHERE clause in LINQ trying to use a wildcard

Posted on 2011-02-28
6
756 Views
Last Modified: 2013-11-11
I am asking the user to key in a code to lookup.  I want them to be able to key in just part of the code and my result come back with the closest match to their partial entry in the field.  Example.

Actual Code: 234.32

They key in:
23   -  it would return in the list
234 - it would return in the list
234. it would return in the list.

Can someone show me where I am goin wrong?


var ICD9Result = from ICD9data in dc1234.ICD9s
                                 where ICD9data.ICD9_cd.ToString().StartsWith("@"+tbDiagCodeLookup.ToString())
                                 select new { ICD9data.ICD9_cd, ICD9data.Name };

Open in new window

0
Comment
Question by:kwh3856
  • 3
  • 2
6 Comments
 
LVL 12

Expert Comment

by:HugoHiasl
ID: 35004544
What for is the @ character?  This one is normally used if you use predefined varaibles what you do not do here.
0
 

Author Comment

by:kwh3856
ID: 35007857
I thought that was the wildcard symbol.  If not...what is a wildcard symbol in LINQ?
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 35007966
Hi Kenny;

The error is that you have prepend a @ to the search string. So what you are sending to the server is @23, @234 and @234.So remove the @ sign. So try it like this:

var ICD9Result = from ICD9data in dc1234.ICD9s
                     where ICD9data.ICD9_cd.ToString().StartsWith(tbDiagCodeLookup.ToString())
                     select new { ICD9data.ICD9_cd, ICD9data.Name };

Open in new window


There is a better way which gives more flexibility and that is to use the T-SQL Like command in the query as follows:

using System.Data.Linq.SqlClient;                 
                 
string pattern = "23%";

var ICD9Result = from ICD9data in dc1234.ICD9s
                 where SqlMethods.Like(ICD9data.ICD9_cd.ToString(), pattern)
                 select new { ICD9data.ICD9_cd, ICD9data.Name };                 

Open in new window



The pattern can be any T-SQL Like meta-characters.

Fernando
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 62

Expert Comment

by:Fernando Soto
ID: 35008262
Hi Kenny;

To your question, "I thought that was the wildcard symbol.  If not...what is a wildcard symbol in LINQ?", When you use the Startswith the function takes your string and adds a % to the end of the string. Using the SqlMethods.Like you place the meta-characters where you like  using the T-SQL Like pattern characters found here.

Fernando
0
 

Author Closing Comment

by:kwh3856
ID: 35009884
Fernando
Thank you very much. I appreciate it when you answer my questions because I am always learning something new. Thanks
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 35009951
Not a problem Kenny, always glad to help.  ;=)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now