Solved

Use Linq to entities to filter on alpha and numeric values

Posted on 2012-03-20
2
606 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

9 Experts available now in Live!

Get 1:1 Help Now