如何在我的具体情况下实施 IEnumerable?
How to implement IEnumerable in my specific case?
我的问题以前有人问过,但是我看之前所有的帖子都想不通。显然我没有理解正确。
我让 Visual Studio 生成了一个 ADO NET Entity Framework 模型,首先从数据库编码。在数据库中,我有一个名为 Finishes 的 table(用于保存游戏的所有可能结局,只是为了澄清)。这一切都很好。现在我需要实现 IEnumerable 以便能够遍历它。至此我明白了一切。我似乎无法以某种方式做到这一点。也许有人可以照亮它,所以我会一劳永逸地理解。
Visual Studio 生成了两个 类;
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?非常感谢您的帮助(可能还有解释)。
尝试使用这种方法:
public IEnumerable<Finish> Get()
{
var query = base.Set<Finish>();
return query.ToList();
}
我的问题以前有人问过,但是我看之前所有的帖子都想不通。显然我没有理解正确。
我让 Visual Studio 生成了一个 ADO NET Entity Framework 模型,首先从数据库编码。在数据库中,我有一个名为 Finishes 的 table(用于保存游戏的所有可能结局,只是为了澄清)。这一切都很好。现在我需要实现 IEnumerable 以便能够遍历它。至此我明白了一切。我似乎无法以某种方式做到这一点。也许有人可以照亮它,所以我会一劳永逸地理解。
Visual Studio 生成了两个 类;
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?非常感谢您的帮助(可能还有解释)。
尝试使用这种方法:
public IEnumerable<Finish> Get()
{
var query = base.Set<Finish>();
return query.ToList();
}