Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

LINQ Contains

Posted on 2011-03-16
1
Medium Priority
?
368 Views
Last Modified: 2012-05-11
I have a checkboxlist that works as a filter, I concatenate the values as a comma separated list like here

 protected void Filter_Click(object sender, EventArgs e)
        {
            Collection<string> items = new Collection<string>();
            for (int index = 0; index < FilterWraps.Items.Count; index++)
            {
                if (FilterWraps.Items[index].Selected)
                {
                    items.Add(FilterWraps.Items[index].Value);
                }
            }
            //here you will get selected values
            string selected = String.Join(",", items.ToArray());
            LoadWraps(Take == null ? 10 : Take, Skip == null ? 10 : Skip, selected);
        }


Then on the linq method I do this


private void LoadWraps(int take, int pageSize, string filter)
        {
            Take = take;
            Skip = pageSize;
            using (Entities.EntitiesDataContext dc = new Entities.EntitiesDataContext(SPContext.Current.Site.Url))
            {
                EntityList<Entities.Wrap> wraps = dc.GetList<Entities.Wrap>("Wraps");
                List<Entities.Wrap> listWraps = new List<Entities.Wrap>();
                if (string.IsNullOrEmpty(filter))
                {
                    var wrapsqry = (from row in wraps
                                    where row.ItemLanguage.ToString() == (WebPart.Language == null ? DefaultLanguage : Language)
                                    orderby row.PublicationDate descending
                                    select row).Take(Take).Skip(Skip).ToList();
                    listWraps = (List<Entities.Wrap>)wrapsqry;
                }
                else
                {
                    var wrapsqry = (from row in wraps
                                    where row.ItemLanguage.ToString() == (WebPart.Language == null ? DefaultLanguage : Language)
                                    && row.WrapType.Value.ToString().Contains(filter)
                                    orderby row.PublicationDate descending
                                    select row).Take(Take).Skip(Skip).ToList();
                    listWraps = (List<Entities.Wrap>)wrapsqry;
                }



but the contains is not working. any idea?
0
Comment
Question by:LuisEstebanValencia
1 Comment
 
LVL 10

Accepted Solution

by:
Nash2334 earned 2000 total points
ID: 35147563
Your contains is reversed, you are checking if the SP item value contains the filter, not if the filter contains the value:

&& row.WrapType.Value.ToString().Contains(filter)

should be

&& filter.Contains(filterrow.WrapType.Value.ToString())

Also, note that ToList() can become a very expensive operation when thousands of items are involved.  Good luck.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

971 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