Solved

LINQ Contains

Posted on 2011-03-16
1
355 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

17 Experts available now in Live!

Get 1:1 Help Now