I
I
Ilya2013-12-26 19:46:58
C++ / C#
Ilya, 2013-12-26 19:46:58

How to execute query in Linq case insensitively?

Hello. I'm trying to make a request like:

string word = search_txbx.Text.;               
                ObservableCollection<Model.Game> games = _ViewModel.Games;
                var query = from db in games
                            where db.name.Contains(word)
                            select db;
                games_dgrd.ItemsSource = query;

Everything is working. But case sensitive. How can I make the query case insensitive.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Illivion, 2013-12-27
@Zerpico

Convert both components being compared to upper or lower case and compare.

D
DaMneD_SoUL, 2013-12-26
@DaMneD_SoUL

Code with minimal changes to solve your problem

string word = search_txbx.Text.;               
                ObservableCollection<Model.Game> games = _ViewModel.Games;
                var query = from db in games
                            where db.name.Contains(word, StringComparer.InvariantCultureIgnoreCase)
                            select db;
                games_dgrd.ItemsSource = query;

I advise you to familiarize yourself with other ways to compare strings
. It seems to me that a logic error was made, you need to assign the ItemsSource to the ObservableCollection games (pre-created by Linq) and not to the IEnumerable obtained as a result of Linq operations

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question