我想使用类似SQL SERVER的"In子句"的东西。以下是我的型号:
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; }
...
}
我使用以下代码来实现LINQ的"In子句",但我得到了错误:
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;
错误:"System.Data.Entity.DbSet"不包含"Contains"的定义,并且最佳扩展方法重载"System.Linq.ParallelEnumerable.Contains(System.LinqParallelQuery,TSource)"具有一些无效参数
编辑:我想在stationevaluations变量中得到那些与所选评价相关的书。
获取与所选电台相关的书籍:
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;
或最佳选择
var result = db.StationEvaluation.Where(/*condition*/)
.SelectMany(s => s.StationEvaluationBooks);
根据您最近的更新,这个怎么样:
var stationEvaluations = db.StationEvaluation.Where(/*condition*/)
.Select(st => st.StationEvaluationID);
var result = from c in db.StationEvaluationBooks
where stationEvaluations.Contains(c.StationEvaluationID)
select c;