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.
Related
This question already has answers here:
Entity Framework - attribute IN Clause usage
(1 answer)
How to use the IN operator in linq
(1 answer)
How to do an "in" query in entity framework?
(3 answers)
Closed 2 years ago.
I am carrying out a project in C# and at the moment I am making the methods to be consumed via API Rest. I want to make an HttpGet that receives a List ids as a parameter.
I want to be able to use the Entity Framework!!
I know that in my Controller the method has this structure
[HttpGet]
public async Task<IActionResult> SearchListIds(IList<long> ids)
{
return Response(_mapper.Map<IList<MyViewModel>>(_repository.GetListIds));
}
My question is how can I do the method in my Repository, I don't know how I can implement the method to make this query in the database, with several ids, I know that in sql it is this way select * from MyTable where id in (1,2,4,5)
For now my repository looks like this:
public ICollection<MyModel> GetListIds(IList<long> ids)
{
// Incomplete example, because I don't know how I can make this return for the database search, I know that in the end I need to add the .ToList ()
return Db.MyModel.AsNoTracking()
}
Can anybody help me? If there's anything wrong with my code, you can tell me too, please.
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));
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();
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())
This question already has answers here:
What's the difference between IQueryable and IEnumerable [duplicate]
(6 answers)
Closed 8 years ago.
Proper difference of IEnumreable and IQueryable .
Why this two interface used and when used?
Whats the benefit used over list?
What other methods consist in IEnumreable and IQueryable?
In what scenario we have to use IEnumreable and IQueryable?
When querying a database, I prefer using a IQueryable because the request is sent when data are needed. I mean this
var users = (from user in db.Users select user ).AsQueryable();
//it doesn't load data yet until you write users.ToList(); or users.Count();