2016-07-20 10 views
2

私の質問は以前に尋ねられましたが、以前のすべての投稿でもわかりません。明らかに私はそれを正しく理解していません。私の特定のケースでIEnumerableを実装する方法は?

私は、Visual Studioでデータベースからコードを取得し、ADO.NETエンティティフレームワークモデルを生成しました。データベースには、Finishesというテーブルがあります(可能なすべての終了をゲームに保存するために、明確にするために)。これはすべて正常に動作します。今私はIEnumerableを反復できるように実装する必要があります。これまで私はすべてを理解しています。私は何とかそれをすることができないようです。多分誰かがその光を照らすことができるので、私は一度だけ理解するでしょう。

Visual Studioでは2つのクラスが生成されています。

Checkoutlist.cs:

namespace Bull.Models 
{ 
using System; 
using System.Data.Entity; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Collections; 

public partial class CheckoutList : DbContext, IEnumerable 
{ 
    public CheckoutList() 
     : base("name=DatastoreConnection") 
    { 
    } 

    public virtual DbSet<Finish> Finishes { get; set; } 


    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Finish>() 
      .Property(e => e.First) 
      .IsFixedLength(); 

     modelBuilder.Entity<Finish>() 
      .Property(e => e.Second) 
      .IsFixedLength(); 

     modelBuilder.Entity<Finish>() 
      .Property(e => e.Third) 
      .IsFixedLength(); 
    } 
} 
} 

そしてFinish.cs:

namespace Bull.Models 
{ 
using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Data.Entity.Spatial; 

public partial class Finish 
{ 
    public int Id { get; set; } 

    public int Total { get; set; } 

    [Required] 
    [StringLength(10)] 
    public string First { get; set; } 

    [Required] 
    [StringLength(10)] 
    public string Second { get; set; } 

    [Required] 
    [StringLength(10)] 
    public string Third { get; set; } 
} 
} 

そこで質問です。私の場合、IEnumerableをどのように実装しますか?あなたの助け(そして、おそらく説明)は非常に高く評価されます。

+2

「コード最初のデータベースから」コードが最初に私は、理解していないデータベース – Marusyk

+3

からコードからデータベースではなく、コードを生成するアプローチである 'Finishes'はすでに' IEnumerableを実装します '。 'CheckoutList'は、' IEnumerable'を実装すべきでない 'DbContext'です。 –

+0

ありがとうMegaTron。あなたが言っていることは、間違った方法を使い始めることです。これが私の問題ですか? –

答えて

2

てみてください、このメソッドを使用する:

public IEnumerable<Finish> Get() 
{ 
    var query = base.Set<Finish>(); 
    return query.ToList(); 
} 
+1

@MegaTronありがとう、これは私が探していたものでした! –

+1

助けてくれてうれしい – Marusyk

関連する問題