How to apply LIKE in Entity Framework [duplicate] - c#

This question already has answers here:
Like in Lambda Expression and LINQ
(6 answers)
Closed 5 years ago.
I'm trying to filter out results based on an input string, and I need to use SQL LIKE operation for that and not a normal comparison. Couldn't find the solution online(probably didn't use the correct search words) :
return _context.Cities.Where(t => t.Name == cityName);
And I need it to do WHERE t.name LIKE '%CityName%' . How do I simulate it here ?

_context.Cities.Where(t=> t.Name.Contains(cityName));

Related

How evaluate a string? [duplicate]

This question already has answers here:
How can I evaluate a C# expression dynamically?
(9 answers)
Closed 3 years ago.
So basically what i have been struggling with is to evaluate sentence in EF Core.
For example if i have:
string condition = "65 < 100 AND 65 > 50"
How can evaluate that?
You can execute SQL String in EF Core, So what you need is to write the whole SQL you want in a sctring and then concatenate this part to it. after that you execute it like this
myDbContextObject.Database.ExecuteSqlCommand(sqlString,params[]);
OR
myDbContextObject.Table.FromSql(sqlString,params[]);

Use a Like expression in Lambda [duplicate]

This question already has answers here:
Case insensitive 'Contains(string)'
(29 answers)
Closed 4 years ago.
I have a Lambda expression that search the column after submitting a form.
It does work when I submit it but its doesn't search the right way I would like to search.
I would like to make it work the same way it search in SQL like statement.
select * FROM tableSearch where subject like '%f5%'
This way even if 'F' is capital it still finds it.
Can this be possible using Lambda expression.
With the below code it only finds it if 'F' is not capital unless i enter 'F5' in subject.
if (!string.IsNullOrEmpty(searchControl.subject))
{
searchList = searchList.Where(x => x.subject.Contains(searchControl.subject)).ToList();
}
why not make them both ToLower:
searchList.Where(x => x.subject.ToLower().Contains(searchControl.subject.ToLower()))
or:
searchList.Where(x => x.subject.IndexOf(searchControl.subject, StringComparison.OrdinalIgnoreCase) >= 0)
You need to use StringComparison as below:
searchList = searchList.Where(x => x.subject.Contains(searchControl.subject,StringComparison.CurrentCultureIgnoreCase)).ToList();
By using that you ignore case sensitivity.

C# Linq syntax naming [duplicate]

This question already has an answer here:
LINQ - Query syntax vs method chains & lambda [closed]
(1 answer)
Closed 5 years ago.
In some cases I see LINQ written this way:
L.Select(_ => _.A).Where(...)
and in some other cases, I see this:
A = from B in C where (...)
Do these two syntaxes have different names?
I understand both, but they seem to be referred to as LINQ so I am a bit confused.
The first one is Method Syntax or Method extension syntax or Fluent
The second one is Query Syntax or Query Expression Syntax

LINQ 2 SQL Using Contains [duplicate]

This question already has answers here:
Using contains() in LINQ to SQL
(7 answers)
Closed 8 years ago.
I am trying to convert the following SQL statement to a Link2SQL statement.
SELECT * FROM Global.CustomData
WHERE CustomDataSource LIKE '%Plugin%'
I have converted it to this statement
var query =
from item in db.CustomDatas
where item.CustomDataSource.Contains(dataSource)
select item;
And have tried setting dataSource to the following: "Plugin", "%Plugin%", "/Plugin/" and "%/Plugin%/". These I have taken from other examples. Unfortunately, although the TSQL statement does return a value, I cannot get the Linq2Sql statement to return anything. Could someone tell me what I am doing wrong?
You should pass "Plugin", the only thing I can think of is the case sensitivity. Try something like this:
where item.CustomDataSource.ToLower().Contains(dataSource.ToLower())

Linq Distinct() is not working [duplicate]

This question already has answers here:
LINQ's Distinct() on a particular property
(23 answers)
Closed 8 years ago.
Distinct() is not working. It displays all the repeating values.
I searched for a solution but just got more confused. I tried this :
var categories = db.Orders.OrderBy(c => c.Item1).ToList().Distinct();
var categories = db.Orders.Distinct().OrderBy(c => c.Item1).ToList();
Is there a quick uncomplicated way to make this work?
use GroupBy instead of Distinct

Categories