2014-02-07 149 views
0

嗨,我正在改變職業生涯到計算機編程。我還在上大學。我必須將以下LINQ查詢從方法語法更改爲查詢語法。得到我的是方法查詢的兩個步驟過程。首先它得到一個teamId,然後它返回一個基於上下文並使用teamId的列表。我很困惑如何將其轉換爲查詢方法。大多數問題都是關於從查詢語法到方法的。將LINQ查詢語法方法語法轉換爲查詢語法

有人可以幫忙嗎?

public IEnumerable<TemplateView> GetTemplates(Guid userId, int languageId) 
    { 
     using (DigigateEntities context = new Models.DigigateEntities()) 
     { 
      var teamId = context 
          .TeamMembers 
          .Include("Team") 
          .FirstOrDefault(c => c.UserId == userId) 
          .Team.Id; 

      return context 
         .TeamTemplates.Include("Template") 
           .Where(c => c.TeamId == teamId) 
           .Select(c => c.Template) 
           .Where(c => c.StatusId == 1/*Active*/) 
            .Select(k => new TemplateView 
            { 
             TemplateName = k.Name, 
             Id = k.Id, 
             IsCustom = k.Custom, 
             TypeId = k.TypeId, 
             TypeName = k.TemplateType.Description, 
             FileName = k.FileName, 
             TemplateImage = "test.png", 
             LanguageId = k.LanguageId, 
             LanguageName = k.Language.Name, 
             CreateDate = k.CreateDate 
            }).ToList(); 
     } 
    } 
+0

你的意思是SQL查詢?你應該用那個標籤。你也可以提供架構,這樣你就可以得到答案。 – Miller

+1

你說得對,我會嘗試添加標籤...謝謝! François – user3284406

+3

@MillerKoijam,LINQ查詢語法與SQL查詢語法不同。 SQL標籤對於這個問題是不合適的。 – danludwig

回答

0

第一個很簡單。我延遲了查詢的執行直到結束。因爲你可能會在你的例子一個空引用異常訪問.FirstOrDefault().Team.Id;

var teamId = (from c in context.TeamMembers.Include("Team") 
      where c.UserId == userId 
      select c.Team.Id).FirstOrDefault(); 

這一個,你只需要使用一個into才能繼續你的查詢語句

return (from c in context.TeamTemplates.Include("Template") 
     where c.TeamId == teamId 
     select c.Template into k 
     where k.StatusId == 1 
     select new TemplateView 
     { 
     TemplateName = k.Name, 
     Id = k.Id, 
     IsCustom = k.Custom, 
     TypeId = k.TypeId, 
     TypeName = k.TemplateType.Description, 
     FileName = k.FileName, 
     TemplateImage = "test.png", 
     LanguageId = k.LanguageId, 
     LanguageName = k.Language.Name, 
     CreateDate = k.CreateDate 
     }).ToList(); 
+0

謝謝大家的回答!非常感激。弗朗索瓦 – user3284406

0
public IEnumerable<TemplateView> GetTemplates(Guid userId, int languageId) 
{ 
    using (DigigateEntities context = new Models.DigigateEntities()) 
    { 
     var teamId = (from tm in context.TeamMembers.Include("Team") 
        where tm.UserId==userId 
        select tm.Id).FirstOrDefault(); 

     IEnumerable<TemplateView> result = from k in (from tmp in context.TeamTemplates.Include("Template") 
                 select tmp.Template) 
              where k.StatusId==1 
              select new 
              { 

               TemplateName = k.Name, 
               Id = k.Id, 
               IsCustom = k.Custom, 
               TypeId = k.TypeId, 
               TypeName = k.TemplateType.Description, 
               FileName = k.FileName, 
               TemplateImage = "test.png", 
               LanguageId = k.LanguageId, 
               LanguageName = k.Language.Name, 
               CreateDate = k.CreateDate 
              }; 
     return result; 
    }