LINQ 2 SQL Using Contains [duplicate] - c#

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

Related

Wrong case sensitivity using linq [duplicate]

This question already has answers here:
LINQ to Entities case sensitive comparison
(8 answers)
Write a search query which is case insensitive in EF Core?
(2 answers)
Closed 1 year ago.
Please help with the following result. I am using linq to perform a query. I know the String.Equals(string) is case sensitive, and I have tested it. However, when I am applying String.Equals functions in the linq statement below, the result is case insensitive. And I can not find what went wrong.
In the result below, I am searching for "qiao", however, "Qiao" was returned as a valid result. And it is not expected. Please help.
for case-sensitive result you can try == operator instead of .Equals() method -
var query = from c in db.Contacts where c.Name == "qiao" select c;
You can use ToUpperCase() or ToLowerCase to solve case sensitivity problem.
Example-
var query = from c in db.Contacts where c.Name.ToUpperCase().Equals("QIAO")
select c;

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

Running a Distinct on a record [duplicate]

This question already has answers here:
Distinct in Linq based on only one field of the table
(10 answers)
Closed 7 years ago.
In LINQ if the result that I have is the a list of whole records from the database and I want to do a Distinct on one column of this record, How do I do that?
You could try something as simple as:
var distincts = records.Select(x=>x.ColumnName).Distinct();

How to work with Conditional Linq Query? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Conditional Linq Queries
We're working on a Log Viewer. The use will have the option to filter by user, severity, etc. In the Sql days I'd add to the query string, but I want to do it with Linq. How can I conditionally add where-clauses?
Assuming you are working with IEnumerable, like this:
IEnumerable<LogMessage> logs = /* whatever your source is */
if(condition) {
logs = logs.Where(log => log.Severity == Severity.Error); // or whatever
}
You can also do this multiple times. If your data source is IQueryable use that instead of IEnumerable.

"Between" in Linq C# [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
LINQ Between Operator
Dear All,
Hi,
I need to write this query in LINQ C#. can anyone help me?
Select *
From Mytable
where MyText BETWEEN 'john' AND 'Pear'
I believe this query should work:
var results = yourTable.Where(x => x.Text.CompareTo("john") > 0 &&
x.Text.CompareTo("Pear") < 0);
This assumes that you want to compare the text in each row of the table, and not some pre-dfined string.
Here is how you can do it with ObjectQuery
MytableSet.Where("it.Name between #start and #end", new ObjectParameter("start", "john"), new ObjectParameter("end", "Pear"))
EDIT:
Forget to mention that this statement is specific to Entity Framework not LINQ2SQL.

Categories