Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-28
6
Medium Priority
?
785 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 64

Accepted Solution

by:
Fernando Soto earned 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 64

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 64

Expert Comment

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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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