?
Solved

Use Linq to entities to filter on alpha and numeric values

Posted on 2012-03-20
2
Medium Priority
?
617 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
[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
2 Comments
 
LVL 58

Accepted Solution

by:
amit_g earned 2000 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

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

801 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