如何在我的具体案例中实现IEnumerable
本文关键字:案例 实现 IEnumerable 我的 | 更新日期: 2023-09-27 17:57:59
以前有人问过我这个问题,但我甚至无法理解之前的所有帖子。很明显,我没有正确理解。
我让Visual Studio生成了一个ADO.NET实体框架模型,代码首先来自数据库。在数据库中,我有一个名为Finishes的表(用来保存游戏的每一个可能的结局,只是为了澄清)。这一切都很好。现在我需要实现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();
}
}
}
和完成.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();
}