Solved

Use Linq to entities to filter on alpha and numeric values

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

839 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