Reputation: 1064
I want to use something like "In clause" of SQL SERVER. Here are my models:
public partial class StationEvaluationBooks
{
public int StationEvaluationBookID { get; set; }
public Nullable<int> StationEvaluationID { get; set; }
public virtual StationEvaluation StationEvaluation { get; set; }
...
}
public partial class StationEvaluation
{
public StationEvaluation()
{
this.StationEvaluationBooks = new HashSet<StationEvaluationBooks>();
}
public int StationEvaluationID { get; set; }
public virtual ICollection<StationEvaluationBooks> StationEvaluationBooks { get; set; }
...
}
I used the following code to implement "In clause" by LINQ but I got error:
StationsEntities db = new StationsEntities();
var stationevaluations = db.StationEvaluation;
//some conditions:
stationevaluations = stationevaluations.Where(/*condition*/);
..
var result = from c in db.StationEvaluationBooks
where stationevaluations.Contains(c.StationEvaluationID)
select c;
Error: 'System.Data.Entity.DbSet' does not contain a definition for 'Contains' and the best extension method overload 'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)' has some invalid arguments
EDIT: I want to get those books that are related to the selected evaluations in stationevaluations variable.
Upvotes: 2
Views: 1765
Reputation: 236208
Getting books related to selected stations:
var ids = db.StationEvaluation.Where(/*condition*/)
.Select(s => s.StationEvaluationID)
.ToList();
var result = from b in db.StationEvaluationBooks
where b.StationEvaluationID.HasValue &&
ids.Contains(b.StationEvaluationID.Value)
select b;
Or best option
var result = db.StationEvaluation.Where(/*condition*/)
.SelectMany(s => s.StationEvaluationBooks);
Upvotes: 5
Reputation: 16137
Based on your most recent update, how about this:
var stationEvaluations = db.StationEvaluation.Where(/*condition*/)
.Select(st => st.StationEvaluationID);
var result = from c in db.StationEvaluationBooks
where stationEvaluations.Contains(c.StationEvaluationID)
select c;
Upvotes: 2