Solved

Use Linq to entities to filter on alpha and numeric values

Posted on 2012-03-20
2
609 Views
Last Modified: 2012-03-20
I have the following code in my method

            MyListViewModel viewModel = new MyListViewModel {
                MyList= repository.MyList
                .Where(m=> alphaFilter == (string)null || m.Title.StartsWith(alphaFilter))

this filters the list based on the first character in the string.  It works perfectly if the value passed as alphaFilter matches the first character of a the m.title.

My problem is that i want an alpha character to filter on the same character, but i want to be able to pass a "#" character and retrieve all entries that start with a numeric value.

in other words, i want my select list of alphaFilter characters to look like this...

# A B C D
not
1 2 3 4 5 6 7 8 9 0 A B C D

I know there is probably an easy solution, but i'm having trouble finding it.  Any help would be much appreciated
0
Comment
Question by:kmitch62
2 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 500 total points
ID: 37743680
What is alphaFilter and how you are populating it. It seems to be string. If so, the posted code won't work if it has more than one characters i.e. alphaFilter = "A" would work but alphaFilter = "AB" won't. Have you tested it for that?

For the digits, you could always do

alphaFilter = alphaFilter.Replace("#", "1234567890");

Open in new window


and then it would work if the rest of the code works.

string alphaFilter = "AB#";
alphaFilter = alphaFilter.Replace("#", "1234567890");

.Where(m=> alphaFilter == null || alphaFilter.Contains(m.Title.Substring(0, 1)))

Open in new window

0
 

Author Closing Comment

by:kmitch62
ID: 37743783
it was being populated by  .Select(x=>x.Title.Substring(0,1)  so there would only be one character. '#' is being added to the list at list[0].  that is the correct behavior.

as for the answer... that's exactly what i needed and I don't believe that i didn't see it myself.  Like i said, a simple answer that i was just missing.  Thanks for the help.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now