首先使用Entity Framework代码存储过程

时间:2015-04-23 21:43:19

标签: asp.net-mvc entity-framework stored-procedures

我有一个带有Entity Framework 6的ASP.NET MVC 5应用程序,我正在使用代码第一种方法。

我的模型定义如下:

public class EntityBase 
{
   [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int Id {get; set; }
}

public class Question : EntityBase
{
    public QuestionTypeEnum QuestionType {get; set; } 
}

public enum QuestionTypeEnum
{
    none = 0,
    Type1 = 1,
    Type2 = 2
    .
    .
}

public class Answer : EntityBase
{
      public Question Answers {get; set; }
      public userInfo userInfo {get; set; } 
      public bool Isvalidated {get; set;}
      public FilterTypeEnum FitlterType
}

public enum FilterTypeEnum 
{
    filter1 = 0,
    filter2 = 1
} 

public class UserInfo : EntityBase
{
      public UserInfoTypeEnum UserInfoType {get; set; }
}

public enum UserInfoTypeEnum 
{
          type1 = 0,
          type2 = 1,
          type3 = 2
} 

public class Company : EntityBase
{
      public List<Answer> Answers {get; set; }
      public userInfo userInfoAdmin {get; set; } 
}

我遇到了一些性能问题,我想将以下方法转换为存储过程:

public List<Answer> FilterCompanyAnswers(FilterTypeEnum filter, List<Answer> companyAnswers)
{
    var answers = companyAnswers.Where(a => ((a.Question.QuestionType != QuestionType.none &&
                                              a.UserInfo != null) &&
                                                  (((a.UserInfo.UserGroup == UserGroupEnum.type1 ||
                                                    (a.UserInfo.UserGroup == UserGroupEnum.type2 && a.IsValidated)) &&
                                                    a.AnswerTypeId == (int)filter &&
                                                   filter == AnswerTypeEnum.filter1) ||
                                                 ((a.UserInfo.UserGroup == UserGroupEnum.type3 ||
                                                   a.UserInfo.UserGroup == UserGroupEnum.type1 ||
                                                   a.UserInfo.UserGroup == UserGroupEnum.type2) &&
                                                   a.FilterTypeEnum  == (int)filter &&
                                                 filter == AnswerTypeEnum.filter2))) ||
                                                 (a.Question.QuestionType == QuestionTypeEnum.none))
                                                 .OrderByDescending(a => a.DateCreate)
                                    .GroupBy(a => a.KeywordId)
                                    .Select(grp => grp.First())
                                    .ToList();

    return answers;
}

提前致谢

0 个答案:

没有答案