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;
}
}
Source: stackoverflow.com
Related Articles
- Error when searching for string containing &
- Error when converting splitted string
- Error when using XDocument in code behind
- string format error when reading CSVs to object with LINQ
- Searching Error in Linq when checking for nulls
- directory.getfiles stops searching when error
- 500 Error when converting nullable DateTime to string in IQueryable
- List or Array of String Contain specific word in Html Source Code
- Getting the Error in my code when framing LINQ
- How do I sort strings alphabetically while accounting for value when a string is numeric?
- C# - code to order by a property using the property name as a string
- Entity-framework code is slow when using Include() many times
- EF Query using .Contains() and .ToLowerInvariant() Results in no matches when match is found at the end of a string
- Entity Framework - An item with the same key has already been added. - Error when trying to define foreign key relationship
- Why does this error occur when using SingleAsync?
- Error message "Operator '.' cannot be applied to operand of type 'lambda expression'" when converting a method to Extension Method?
- Linq functions give strange compile error when ambiguous use of IEnumerable - possible workarounds?
- Reference required error only when using LINQ
- LINQ to SQL - Compile error when extending data context with partial class and methods
- Entity Framework Linq Query to List - Error when using contains: Only primitive types, enumeration types and entity types are supported
- convert int to string in linq for searching
- Linq to SQL .Equals returns true when comparing int to string --> Id.Equals("5") returns true
- How to reuse a linq expression for 'Where' when using multiple source tables
- .NET String parsing performance improvement - Possible Code Smell
- XDocument.Element returns null when parsing an xml string
- Avoiding code repetition when using LINQ
- Is it possible to evaluate a string containing valid LINQ dynamically / at runtime?
- Why do I get a "key already added" error when using the key from a GroupBy in ToDictionary?
- Forcing Entity Framework to not generate NCLOB's when building Linq-to-Sql Code (Model First)
- Space character at the end of a string when saving to the database
- How to speed up linq query C#
- How to retrieve data from XML using LINQ in VB.NET?
- Group by Time(Only hrs part) in linq
- Creating an IDictionary<string,Expression> proving difficult
- How to find out if an object's type implements IEnumerable<X> where X derives from Base using Reflection
- LIKE with Linq to Entities
- How to do a primary key to equal to an array of int[] in LINQ?
- Linq check value for equality in collection and assign value to other collection
- Linq-to-entities - Include() method not loading
- Entity Framework Check Nested Null Objects
- Entity Framework List Contains in lambda
- How to make all columns allow null before adding a new row?
- How add expression of where for generic type?
- Entity Framework / LINQ / .NET - Perform 'LIKE' on Date
- Read and Write XML in C# using Linq
- Convert linq query expression with multiple froms into extension method syntax
- LINQ syntax question
- Entity Framework + LINQ Expression outer join error
- convert pair of arrays to array of pairs (using LINQ if possible)
- Check for duplicates in array of custom type using Linq in C#