Hello,
Try like this:
public string[] GerReserveby(List<int> ids, string userName, bool reserveOn)
{
string[] reservedBy = null;
using (var db = new NamesTestDbContext())
{
reservedBy = db.FleetNames.Where(x => ids.Contains(x.Id) && (!x.ReservedBy.Equals(userName) && !reserveOn)).Select(x => x.ReservedBy).Distinct().ToArray();
}
return reservedBy;
}
In your method you're iteraing all
ids and making query in every loop pass. It will take much longer time to get all data especially when you can get all data with one query.
I don't know your DB structure but I think that will get you the point :)
You can find many tutorial on CRUD operations here on CodeProject.
[Update - For comment]
To retrieve reservedBy names without duplicates use Distinct method. Code updated.
Good luck!