2009-04-23 77 views
1

下面的代码是我现在有的并且工作正常。我觉得我可以在Linq而不是C#代码中完成更多的工作。C#Linq代码重构

有没有人可以用更多的Linq代码和更少的C#代码来完成相同的结果。

public List<Model.Question> GetSurveyQuestions(string type, int typeID) 
    { 
     using (eMTADataContext db = DataContextFactory.CreateContext()) 
     { 
      List<Model.Question> questions = new List<Model.Question>(); 
      List<Linq.Survey_Question> survey_questions; 
      List<Linq.Survey> surveys = db.Surveys 
              .Where(s => s.Type.Equals(type) && s.Type_ID.Equals(typeID)) 
              .ToList(); 

      if (surveys.Count > 0) 
      {     
       survey_questions = db.Survey_Questions 
            .Where(sq => sq.Survey_ID == surveys[0].ID).ToList(); 

       foreach (Linq.Survey_Question sq in survey_questions) 
       { 
        Model.Question q = Mapper.ToBusinessObject(sq.Question); 
        q.Status = sq.Status; 
        questions.Add(q);       
       } 
      } 
      else 
      { 
       questions = null; 
      } 
      return questions; 
     } 
    } 

这里是我的实体我映射功能,以商务对象

internal static Model.Question ToBusinessObject(Linq.Question q) 
     { 
      return new Model.Question 
      { 
       ID = q.ID, 
       Name = q.Name, 
       Text = q.Text, 
       Choices = ToBusinessObject(q.Question_Choices.ToList())     
      };    
    } 

我希望我的映射功能可按映射问题的状态,像这样。

internal static Model.Question ToBusinessObject(Linq.Question q) 
    { 
     return new Model.Question 
     { 
      ID = q.ID, 
      Name = q.Name, 
      Text = q.Text, 
      Choices = ToBusinessObject(q.Question_Choices.ToList()), 
      Status = q.Survey_Questions[?].Status 
     }; 
    } 

问题是这个函数不知道从哪个调查中拉取状态。

而不是创建的BIZ对象然后像设置Status属性在foreach循环的这样

foreach (Linq.Survey_Question sq in survey_questions) 
{ 
    Model.Question q = Mapper.ToBusinessObject(sq.Question); 
    q.Status = sq.Status; 
    questions.Add(q);       
} 

我想以某种在第q对象调用方法筛选EntitySet<Survey_Question>以上,使得将有只能是q.Survey_Questions [?]集合中的一个项目。

下面

是我的数据库架构和业务对象模式 alt text http://i41.tinypic.com/1051n28.png alt text http://i43.tinypic.com/awua2v.png

+0

链接是C#。不知道你为什么这么做 – Surya 2009-04-23 23:51:48

回答

1

我需要做的是建立一个连接。

public List<Model.Question> GetSurveyQuestions(string type, int typeID) 
    { 
     using (eMTADataContext db = DataContextFactory.CreateContext()) 
     { 
      return db.Survey_Questions 
        .Where(s => s.Survey.Type.Equals(type) && s.Survey.Type_ID.Equals(typeID)) 
        .Join(db.Questions, 
           sq => sq.Question_ID, 
           q => q.ID, 
           (sq, q) => Mapper.ToBusinessObject(q, sq.Status)).ToList(); 
     } 
    } 

然后重载我映射功能

internal static Model.Question ToBusinessObject(Linq.Question q, string status) 
    { 
     return new Model.Question 
     { 
      ID = q.ID, 
      Name = q.Name, 
      Text = q.Text, 
      Status = status, 
      Choices = ToBusinessObject(q.Question_Choices.ToList()), 
     }; 
    } 
0
from question in db.Survey_Questions 
    let surveys = (from s in db.Surveys 
     where string.Equals(s.Type, type, StringComparison.InvariantCultureIgnoreCase) && 
     s.Type_ID == typeID) 
where surveys.Any() && 
surveys.Contains(s => s.ID == question.ID) 
select new Mapper.Question 
{ 
    ID = question.Id, 
    Name = question.Name, 
    Text = question.Text, 
    Choices = ToBusinessObject(question.Question_Choices.ToList()), 
    Status = question.Status 
} 

这是否让你在正确的轨道上?

0

你为什么复制你的所有类?你可以用你的业务逻辑来扩展LINQ to SQL类 - 它们是部分类。这有点违背了OR映射器持久化商业实体的目的。