Solved

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

Posted on 2011-02-28
6
753 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

746 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