score:0
This is a very late answer.
You can use Regex
to solve your problem
Here's what I have tried, hope it helps
I created a sample class
public class SampleTable
{
public string Name { get; set; }
public SampleTable(string name)
{
Name = name;
}
}
Populated sample data
List<SampleTable> sampleTblList = new List<SampleTable>();
sampleTblList.Add(new SampleTable(" Apple"));
sampleTblList.Add(new SampleTable(" APPLE"));
sampleTblList.Add(new SampleTable("Apple"));
sampleTblList.Add(new SampleTable("apple"));
sampleTblList.Add(new SampleTable("apple "));
sampleTblList.Add(new SampleTable("apmangple"));
Solution:-
string fruitName = "apple";
List<SampleTable> sortedSampleTblList = sampleTblList.Where(x =>
Regex.IsMatch(fruitName, x.Name, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase)).ToList();
Output:-
string ans = String.Join(",", sortedSampleTblList.Select(x => x.Name.Replace(" ","_")).ToArray());
Console.Write(ans);
_Apple,_APPLE,Apple,apple,apple_
score:0
fruitsTable.Where(row => row.name.Trim().Equals(fruitname, StringComparison.OrdinalIgnoreCase));
should do what you need, but I'm confused because you've listed almost the same under Issue 3. Were you not realising it was working because you are reusing fruits2
?
This little NUnit test is passing
[Test]
public void FruitTest()
{
var fruitsTable = new List<string> { " Apple", " APPLE", "Apple", "apple", "apple ", " apple", "APPLE " };
var fruitname = "apple ".Trim();
var fruits = fruitsTable.Where(row => row.Trim().Equals(fruitname, StringComparison.OrdinalIgnoreCase));
Assert.AreEqual(fruitsTable.Count(), fruits.Count());
}
score:1
Here's a nice String Extension method that builds on the solutions from a similiar question about casing StackOverflow
Keep in mind, we want to allow for NULL strings in our trim scenarios, so this extension will do a Case Insensitive compare on Trimmed strings after checking for null values
public static class StringExtension
{
// Trim strings and compare values without casing
public static bool SqlCompare(this string source, string value)
{
// Handle nulls before trimming
if (!string.IsNullOrEmpty(source))
source = source.Trim();
if (!string.IsNullOrEmpty(value))
value = value.Trim();
// Compare strings (case insensitive)
return string.Equals(source, value, StringComparison.CurrentCultureIgnoreCase);
}
}
Here's how to use the Extension in your LINQ statement:
(SysUserDisplayFavorites table is composed of char() fields with space filled results. These will get trimmed and compared (case insensitive) to the user provided values in displayFavorite object)
var defaultFavorite = _context.SysUserDisplayFavorites
.Where(x => x.UserId.SqlCompare(displayFavorite.UserId))
.Where(x => x.ModuleCode.SqlCompare(displayFavorite.ModuleCode))
.Where(x => x.ActivityCode.SqlCompare(displayFavorite.ActivityCode))
.Where(x => x.ActivityItemCode.SqlCompare(displayFavorite.ActivityItemCode))
.Where(x => x.IsDefault);
Source: stackoverflow.com
Related Articles
- Relax C# LINQ String Comparison (Trim, Case Insensitive, ??)
- Case insensitive string compare in LINQ expression
- LINQ string contains another string case insensitive
- Linq performing case insensitive comparison
- C# Expression class method extension, making string comparison case insensitive
- linq case insensitive and trimmed comparison
- How can I do case insensitive string search with Linq and SQL Server?
- Check String contain all elements in list using Linq and case insensitive
- What ways exist with linq to compare if a case insensitive and culture using string exists within another
- LINQ Contains Case Insensitive
- Case insensitive string compare in LINQ-to-SQL
- linq case insensitive (without toUpper or toLower)
- Issues Doing a String Comparison in LINQ
- How to write EF in linq to include comparison by lower case
- can linq expression be case insensitive
- LINQ to DataSet case insensitive group by
- Case insensitive Contains with Dynamic Linq
- Entity Framework LINQ contains not case insensitive
- Entity Framework and Case Insensitive String Search
- Case insensitive string compare with linq-to-sql and linq-to-objects
- Linq case insensitive join
- Dynamic LINQ DateTime Comparison String Building - Linq To Entities
- Net core EF 3.1 LINQ string comparison no longer working
- how to build case insensitive strong typed LINQ query in c#?
- LINQ Source Code Available
- MongoDB c# driver: Case Insensitive Compare using in or contains on a list using linq
- String Comparison in LINQ to SQL result null
- == by default is case insensitive in c# linq
- creating Linq to sqlite dbml from DbLinq source code
- Linq expression tree string comparison
- Linq using Select and an Indexer
- read xml file with multiple elements to a list in asp.net c#
- When Where clause is used inside Linq statement produces different results than when used outside
- Bind result from a Web API Projection request to a WPF Control like DataGrid or ListView
- Linq using TakeWhile in a query
- How to filter List Inside List using Linq?
- Using GroupBy to pick rows in Entity Framework
- Rendering IEnumerable<string> from an entity framework generic repository
- Is it possible to create ICriteria/ICriterion from LINQ or HQL?
- When are collections enumerated (IEnumerable)
- Displaying an IGrouping<> with nested ListViews
- OrderBy, ThenBy and IOrderedEnumerable<T>
- C# Retrieve Common Records by LINQ
- queries in entity framework
- Show record using entity framework or linq
- List<T> to a Dictionary
- C# - Linq : Unable to create a constant value of type Only primitive types or enumeration types are supported in this context.
- How to match a enum value with some enum values using linq
- How to count below extensions? by using c#
- How to query a List<T>?