IEnumreable and IQueryable [duplicate] - c#

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

Related

Perform a Get that takes List <long> ids as parameters [duplicate]

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.

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

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

Get distinct parameters from a List [duplicate]

This question already has answers here:
Get distinct items from a list
(4 answers)
Linq Distinct() by name for populate a dropdown list with name and value
(7 answers)
LINQ: Distinct values
(8 answers)
Closed 9 years ago.
I have a CarData object with the following properties:
PrimaryKey Make Model Year Drivetrain Country
I have about 1000 of these CarData objects in a List :
List<CarData> CarObjects
Is there a simple way to get a list of the distinct Makes?
var makes = CarObjects.Select(car => car.Make).Distinct();
This transforms the list from a list of CarData to a list of Makes, and then just finds the distinct values of the new list.
You can use Linq:
CarObjects.Select ( c => c.Make ).Distinct().ToList()
var makeList = CarObjects.Select(a => a.Make).Distinct();
Or
List<MakeEnum> = CarObjects.Select(a => a.Make).Distinct().ToList();
As an extra bit of advice, you may want to consider having Make be an enum, since there are (presumably) a finite (and rather small) number of possible makes of cars, instead of piling them into Strings. (You don't make a mention of what kind of property Make is, so maybe you are already doing this).

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.

Categories