Solved

LINQ Contains

Posted on 2011-03-16
1
352 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 500 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

12 Experts available now in Live!

Get 1:1 Help Now