C# Linq syntax naming [duplicate] - c#

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

Related

See generated SQL for LINQ method syntax query? [duplicate]

This question already has answers here:
How to view LINQ Generated SQL statements?
(8 answers)
Get SQL code from an Entity Framework Core IQueryable<T>
(10 answers)
Closed 11 months ago.
I have a fairly simple LINQ query (using method syntax):
_context.Foo.Count(c => c.Bar > 123);
I need to see the SQL that will run against the database. I'm not using SQL Server, so I unfortunately can't use the SQL Server Profiler.
I'm unable to use query syntax in this case.
Is there any sort of .ToQueryString(); method?

How to understand expressions as method parameters in C# [duplicate]

This question already has answers here:
Why would you use Expression<Func<T>> rather than Func<T>?
(12 answers)
Closed 3 years ago.
I am starting to see methods like the one below used more and more, but it's a concept that I don't fully understand.
public virtual Task<List<T>> GetAsync(Expression<Func<T, bool>> exp)
{
using (var conn = _factory.OpenDbConnection())
{
return conn.SelectAsync(exp);
}
}
Can someone help me translate the method parameter there that is an Expression? Like explain it as how it differs from a standard instance parameter?
These are called Expression Trees (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/expression-trees/).
Basically it's a lamdba that can be translated to another platform, from the looks of it some kind of database in your case. This function would be translated (by a library) to SQL and then executed in the database.
Within the code of your program you would generally only need lambdas (Func<>), but in some cases you need an Expression Tree. Besides your example, sometimes you need a dynamically constructed function which can be done using these.
General information on lambdas can be found here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/lambda-expressions

How to apply LIKE in Entity Framework [duplicate]

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));

shorthand for ctor delegate [duplicate]

This question already has answers here:
Call constructor as a function in C#
(3 answers)
Closed 7 years ago.
I have the following lambda expression:
Something((o, i) => new TheNewSwiss(o, i));
Is there a shorthand syntax? Something simliar to this but for ctors?
Something((o, i) => TheNewSwiss.New(o,i));
Something(TheNewSwiss.New);
No, there's no equivalent of method group conversions for constructors. (I can see it being handy, but it doesn't exist.) You just need to use the lambda expression syntax you've got in your first snippet.

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())

Categories