0

在下面的 QueryableEntities 方法中, IEnumerable<Competition>.AsQueryable()将返回IQueryable<Competition>由于期待而无法返回的IQueryable<T>

我只能写一种方法来返回IQueryable<Competition>IQueryable<Submission>

如果有人知道怎么做,请告诉我,谢谢

public partial class WinBoutsDataContext<T> : System.Data.Linq.DataContext where T : class
{
    public IQueryable<T> QueryableEntities
    {
        get
        {
            if (typeof(T).Equals(typeof(Model.Competition)))
            {
                return this.ConvertToCompetitionList(
                      this.GetTable<Storage.Competition>()).AsQueryable();
            }
             else if(typeof(T).Equals(typeof(Model.Submission)))
            {
                return this.ConvertToSubmissionList(
                       this.GetTable<Storage.CompetitionEntry>()).AsQueryable();
            }
        }
    }

    public DataAccess.Model.Competition ConvertToCompetition(DataAccess.Storage.Competition comp)
    {

        DataAccess.Model.Competition modelComp = new DataAccess.Model.Competition 
        { 
            CompetitionID = comp.CompetitionID, 
            Active = comp.Active, 
            CompetitionStatusID = 
            comp.CompetitionStatusID, 
            Duration = comp.Duration, 
            EndDate = comp.EndDate, 
            Entrants = comp.Entrants, 
            Featured = comp.Featured, 
            Image = comp.Image, 
            IsOvertime = comp.IsOvertime, 
            MaxEntrants = comp.MaxEntrants, 
            Notes = comp.Notes, 
            OvertimeDuration = comp.OvertimeDuration, 
            OvertimeEnd = comp.OvertimeEnd, 
            OvertimeStart = comp.OvertimeStart, 
            ParentCompetitionID = comp.ParentCompetitionID, 
            Prize = comp.Prize, StartDate = comp.StartDate, 
            SuggestedBy = comp.SuggestedBy, 
            Summary = comp.Summary, 
            Title = comp.Title 
        };

        return modelComp;
    }

    public IEnumerable<DataAccess.Model.Competition> ConvertToCompetitionList(IEnumerable<DataAccess.Storage.Competition> compList)
    {
        List<DataAccess.Model.Competition> compModelList = new List<DataAccess.Model.Competition>();

        foreach (DataAccess.Storage.Competition comp in compList)
        {
            compModelList.Add(ConvertToCompetition(comp));
        }

        return compModelList;
    }
}
4

2 回答 2

2

同意阿德里安对设计原则的评论,但如果你想这样做

public IQueryable<T> QueryableEntities
{
    get
    {
        if (typeof(T).Equals(typeof(Model.Competition)))
        {
            return this.ConvertToCompetitionList(
                  this.GetTable<Storage.Competition>()).AsQueryable() as IQueryable<T>;
        }
         else if(typeof(T).Equals(typeof(Model.Submission)))
        {
            return this.ConvertToSubmissionList(
                   this.GetTable<Storage.CompetitionEntry>()).AsQueryable() as IQueryable<T>;
        }
    }
}

将编译并运行良好。这里x as IQueryable<T>给出xIQueryable<T>是否可以转换,null如果不能转换。

注意:这意味着当你构造你的泛型类时,如果它有类型Model.Competition,它将返回IQueryable<Model.Competition>,如果它有类型Model.Submission,它将返回IQueryable<Model.Submission>,并且将返回null任何其他类型。

于 2012-11-07T10:09:22.083 回答
0

通常最好不要一概而论。许多开发人员似乎花了很多时间试图让一个属性/方法/类做很多事情,但这违背了许多面向对象的原则。就我个人而言,我只会使用 2 个属性,可能称为Competitions另一个Submissions. 这将比 . 透露更多意图QueryableEntities

于 2012-11-07T09:31:43.547 回答