gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:31:58 25 4
gpt4 key购买 nike

我想使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com