2

我想使用 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”不包含“包含”的定义,并且最佳扩展方法重载“System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)”有一些无效参数

编辑: 我想在 stationevaluations 变量中获取与所选评估相关的书籍。

4

2 回答 2

5

获取与所选电台相关的书籍:

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);
于 2013-02-11T14:06:21.890 回答
2

根据您最近的更新,这个怎么样:

var stationEvaluations = db.StationEvaluation.Where(/*condition*/)
                                             .Select(st => st.StationEvaluationID);

var result = from c in db.StationEvaluationBooks
                       where stationEvaluations.Contains(c.StationEvaluationID)
                       select c;
于 2013-02-11T14:02:21.970 回答