Linq Contains without considering accents [duplicate] - c#

This question already has an answer here:
Complex "Contains" string comparison
(1 answer)
Closed 7 years ago.
I need to get all the results where the text contains a particular word ignoring all accents.
Now I have the following:
filtered = result.Where(p => p.#string.ToString().ToUpper().Contains(word));
Or a simplified version:
filtered = result.ToUpper().Contains(word));
How can I make the "Contains" statement ignore the accents?
Thanks in advance

Borrowing a similar solution form here:
string[] result = {"hello there", "héllo there","goodbye"};
string word = "héllo";
var compareInfo = CultureInfo.InvariantCulture.CompareInfo;
var filtered = result.Where(
p => compareInfo.IndexOf(p, word, CompareOptions.IgnoreNonSpace) > -1);

You want to use the StringComparison.InvariantCultureIgnoreCase enum.
Source https://msdn.microsoft.com/en-us/library/system.stringcomparison(v=vs.110).aspx
filtered = result.Contains(word, StringComparison.InvariantCultureIgnoreCase);
However this is only going to work with LinqToObject. If you are using LinqToSQL or LinqToEntityFramework or LinqToNHibernate, this will not work.

Related

How do i make .Any with combination of .Contains non-case sensitive [duplicate]

This question already has answers here:
LINQ Contains Case Insensitive
(11 answers)
Closed 3 months ago.
I have the following code
query = query.Where(x => words.Any(x.Message.Contains));
words is a string[] and x.Message is a string
I would like to filter out my query based on all the words in my array but I would like this not to be case sensitive comparison so if i type 'bob' or 'BOb' it should not care and still compare those words against the message if Message is 'BOB is awesome' or 'bob is awesome'
A better option is to use Contains overload with StringComparison parameter:
query = query.Where(x => words.Any(s => x.Message
.Contains(s, StringComparison.InvariantCultureIgnoreCase)));
This will compare the list of strings to the message as you desire. I use .ToLower() to convert both the string and each word in the list so it's essentially case-insensitive.
query = query.Where(x => words.Any(s => x.Message.ToLower().Contains(s.ToLower())));
Check if a string contains an element from a list (of strings): Check if a string contains an element from a list (of strings)

How to sort a list of string that have numbers within them as well using Linq only [duplicate]

This question already has answers here:
Natural Sort Order in C#
(18 answers)
Closed 7 months ago.
I have a flyerPages list which I pull from my database into a list as seen here:
var flyerPages = _flyerPageRepository.FindAllPagesAsync(_flyerId);
I then do the following to order them:
flyerPages.OrderBy(x => x.DocumentName);
This is my result:
DE9320_1_1_.txt
DE9320_1_2_.txt
DE9320_10_1_.txt
DE9320_10_2_.txt
DE9320_11_1_.txt
DE9320_11_2_.txt
DE9320_4_1_.txt
DE9320_4_2_.txt
DE9320_5_1_.txt
DE9320_5_2_.txt
DE9320_6_1_.txt
DE9320_6_2_.txt
DE9320_7_1_.txt
DE9320_7_2_.txt
DE9320_8_1_.txt
DE9320_8_2_.txt
The first number in the filename represents the page number the second represents the part of the page so DE9320_1_1_.txt is page 1 part 1, it can also be shown as DE9320_01_1_.txt etc. Sometimes there is only one part and it is shown as DE9320_1.txt or DE9320_01.txt.
Because I am using a string the 10th page comes before the second. Now of course I can just grab the numbers from the middle and sort them but I was wondering is there a way to use only a linq statement to achieve this? If not what would be an easier method then going through each string and extracting the numbers from the middle?
Is this what your are looking for
string[] inputs = {
"DE9320_1_1_.txt",
"DE9320_1_2_.txt",
"DE9320_10_1_.txt",
"DE9320_10_2_.txt",
"DE9320_11_1_.txt",
"DE9320_11_2_.txt",
"DE9320_4_1_.txt",
"DE9320_4_2_.txt",
"DE9320_5_1_.txt",
"DE9320_5_2_.txt",
"DE9320_6_1_.txt",
"DE9320_6_2_.txt",
"DE9320_7_1_.txt",
"DE9320_7_2_.txt",
"DE9320_8_1_.txt",
"DE9320_8_2_.txt"
};
string[] sorted = inputs.Select(x => new { filename = x, splitArray = x.Split(new char[] { '_' }) })
.OrderBy(x => x.splitArray[0])
.ThenBy(x => int.Parse(x.splitArray[1]))
.ThenBy(x => int.Parse(x.splitArray[2]))
.Select(x => x.filename)
.ToArray();

Find elements from collection contains names from string array [duplicate]

This question already has answers here:
Linq Select All Items Matching Array
(2 answers)
Linq filter List<string> where it contains a string value from another List<string>
(4 answers)
Closed 4 years ago.
How can I create LINQ expression to find elements from collection contains names from string array?
string[] names = ["John", "Hanna", "Bill", "Donald"];
I've created expression like below but it is not correct. How can I fix that?
result = (x => x.CompanyEmployeeName.Contains(names));
If you want check if names contains x.CompanyEmployeeName, you'll want to use:
result = something.Where(x => names.Contains(x.CompanyEmployeeName));
let myCollection be the collection of a custom class having a property Name. you have to get all objects from that collection based on the condition that object's name should be available in the names array. Then You can try this:
var filteredItems = myCollection.Where(x=> names.Any(y=>y == x.Name));
I have added a working example here
In your LINQ, you should have a collection first.
Ex: if you have a list: listCompanyEmployee then you can use bellow expression:
var result = listCompanyEmployee.Where(x => names.Contains(x.CompanyEmployeeName));
You need to reverse it. Check if the names contains the employee
var result = db.CompanyEmployee.Where(x => names.Contains(x.CompanyEmployeeName));
One other option, which is prefered if the list in context are larger then your sample data, is to use Join
var result = db.CompanyEmployee.Join(names, x=> x.CompanyEmployeeName, n => n, (x,n)=> n);
You can use Array.Exists. Example if you want to check if names contains CompanyEmployeeName:
result = something.Where(x => Array.Exists(names, name => name == x.CompanyEmployeeName));
and you can use Array.IndexOf like:
result = something.Where(x => Array.IndexOf(names, x.CompanyEmployeeName) != -1);

Filter a nested collections with LINQ "Include" [duplicate]

This question already has answers here:
EF: Include with where clause [duplicate]
(5 answers)
Closed 4 years ago.
I searched online for a long time, but I couldn't really get a straight answer to this. How would I use Include to load a nested collection using Where to filter it? Let's say, for instance, that I want to include all the CartItems that are not disabled:
var myCart = _dbContext.Carts
.Include(cart => cart.CartShippingBoxes
.Select(cartShippingBox => cartShippingBox.CartItems
.Where(cartItem => !cartItem.IsDisabled))); // This doesn't work
Try this:
var carts = _dbContext.CartShippingBoxes
.Include(item => item.Cart)
// .ThenInclude(cart => cart.A)
.Where(cartItem => !cartItem.IsDisabled))
Select(s=> s.Cart).ToList();
Or use overload of Include that takes parameter of type string like:
.Include("Cart").Include("Cart.CollectionA").Include("Cart.CollectionB")
.Include("Cart.CollectionZ")

Find elements which aren't in other list [duplicate]

This question already has answers here:
Compare the difference between two list<string>
(3 answers)
Closed 8 years ago.
I am trying to find values of string which aren't includes into other list.
I have list like:
var list1 = new List<string>();
list1.Add("element1");
list1.Add("element2");
var list2 = new List<string>();
list2.Add("element1");
list2.Add("element2");
list2.Add("element3");
I need to find elements from list2 which aren't in list1, so the result should be only:element3. I tried to do something like right join with LINQ, but it was unsuccessful. Maybe someone know proper solution for this?
Use Enumerable.Except
Produces the set difference of two sequences by using the default
equality comparer to compare values.
var result = list2.Except(list1);
Another way of doing that could be:
var result = list2.Where(r => !list1.Contains(r));
If you need a List<string> as result, then just add ToList().
list1.Except(list2)
Returns the set difference between 2 IEnumerables: MSDN
Use Except
var list3 = list2.Except(list1);
list2.Except(List1)
.Except() should work

Categories

Resources