2

SQLSERVERの「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変数で選択された評価に関連する本を取得したいと思います。

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 に答える