score:1

Accepted answer

I could not reproduce it, i guess it's happening before this code. Could you materialize ss before filtering, like this, to check if code sample is isolated?

var itemsToFilter = ss.ToList();
string str = FilterAllSource;
            if (!string.IsNullOrEmpty(str))
            {

                itemsToFilter = itemsToFilter.Where(it => (it.SourceCode.ToUpper()
                                           .Contains(str.ToUpper())
                                     || it.SourceName.ToUpper()
                                           .Contains(str.ToUpper())));
            }

            if (top > 0)
                itemsToFilter = itemsToFilter.Take(top);

            return itemsToFilter.ToList();

I've tried this with linq to objects, worked fine:

class Program
{
    static void Main(string[] args)
    {
        var ss = new List<Dummy> { new Dummy
                     {
                         SourceCode = "m & m",
                         SourceName = "blabla"
                     }}.AsEnumerable();

        string str = "m & m";
        if (!string.IsNullOrEmpty(str))
        {
            ss = ss.Where(it => (it.SourceCode.ToUpper()
                                       .Contains(str.ToUpper())
                                 || it.SourceName.ToUpper()
                                       .Contains(str.ToUpper()))).ToArray();
        }

        var top = 2;

        if (top > 0)
            ss = ss.Take(top).ToArray();

        Console.WriteLine(ss.Count());
    }
}

public class Dummy
{
    public string SourceCode { get; set; }
    public string SourceName { get; set; }
}

Also tried with linq to sql, not same code, but should raise same problem (if there was problem):

class Program
{
    static void Main(string[] args)
    {
        string str = "&";
        using (var context = new DataClasses1DataContext())
        {
            var clients = context.Clients.Where(x => x.Code.Contains(str.ToUpper()));
            clients = clients.Take(5);
            Console.WriteLine(clients.Count());
        }          
    }
}

score:2

This works for me...

class Program
{
    static void Main(string[] args)
    {
        Program p = new Program();
        List<Test> list = p.GetList();
    }

    public List<Test> GetList()
    {
        List<Test> ss = new List<Test>();
        ss.Add(new Test("m & mm & mm & mm & mm & mm & mm & m", "m & mm & mm & mm & mm & mm & mm & m"));
        string str = "m & m";

        if (!string.IsNullOrEmpty(str)) 
        { 
            ss = ss.Where(it => (it.SourceCode.ToUpper().Contains(str.ToUpper()) || it.SourceName.ToUpper().Contains(str.ToUpper()))).ToList();
        } 

        //if (top > 0) 
        //{
        //    ss = ss.Take(top); 
        //}

        return ss.ToList(); 
    }
}

public class Test
{
    public Test(string sourceCode, string sourceName)
    {
        this.SourceCode = sourceCode;
    }

    public string SourceCode
    {
        get;
        set;
    }

    public string SourceName
    {
        get;
        set;
    }
}

Related Articles