asp.net-mvc - "In clause"通过 LINQ to SQL

标签 asp.net-mvc linq entity-framework

我想使用 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.Linq.ParallelQuery, 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);

关于asp.net-mvc - "In clause"通过 LINQ to SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14813444/

相关文章:

asp.net-mvc - 使用ServiceStack和SimpleInjector一起抵抗API服务

ajax - 如果 AJAX 请求内容长度超过允许的最大长度,则在 MVC 中使用 HttpModule 返回新响应

c# - 如何在 ApiController 中为单个方法返回 JSON?

entity-framework - Entity Framework - 我应该从 BLL 还是 DAL 调用 DbContext

entity-framework - Entity Framework 中的存储过程

javascript - AJAX 成功时未接收数据,结果显示为 "undefined"

c# - IEnumerable<T>.包含谓词

c# - 使用 Linq 优化 WMI 查询

c# - 使用 LINQ 在 C# 中使用 MasterMind 评分算法

entity-framework - 实体类型<class>不是当前上下文模型的一部分