Solved

LINQ Contains

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

631 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