2013-02-11 111 views
0

我有下面的SQL查詢,我想轉換爲Linq,但不能得到它正好工作。無法獲得工作linq子查詢

select l.nid, 
     l.sName, 
     l.language, 
     coalesce(p.kLanguage, 0) kLanguage 

    from vLanguage l 
     left join 
      (
       select pl.kLanguage, 
         p.nid, 
         p.sName 

       from vProductLanguage pl 
        left join vProduct p 
          on pl.kProduct = p.nid 
       where p.nid = 1 
      ) p 
    on l.nid = p.kLanguage 
    where l.bClosed =0 

我在WCF服務

[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "ProductLanguageList")] 
    public List<LookUpProductLanguage> GetProductLanguageList() 
    { 
     var passedProductId = int.Parse("12"); 
     var query = from languageEntity in _languageEntityRepository.AsQueryable() 
        join subQueryResult in (from productLanguageEntity in _productLanguageEntityRepository.AsQueryable() join productEntity in _productRepository.AsQueryable() on productLanguageEntity.LanguageProductId equals productEntity.Id into joinedProductLanguage 
              from productLanguageJoin in joinedProductLanguage.DefaultIfEmpty() where productLanguageJoin.Id.Equals(passedProductId) 
              select new {LanguageId = productLanguageEntity.LanguageId} 
              ) on languageEntity.Id equals subQueryResult.LanguageId 

     return null; 
    } 

因爲我已經返回null,但希望回到在SQL查詢中提到的列的那一刻管理,以便在這裏。我在line join subQueryResult附近收到錯誤「無法從查詢中推斷出類型參數」。我在這是要幹嘛?請糾正我,因爲我確信我做錯了什麼。

+0

任何線索?請幫助 – DevelopmentIsMyPassion 2013-02-11 16:52:53

+0

嘗試在查詢結尾添加'.ToList()'(將整個查詢先包裝在parantheses中 - 就像這樣'var query =(from ...)。ToList();' – Tim 2013-02-11 17:37:45

+0

@Tim我不是即使在添加括號之後也可以添加.ToList()選項,請幫助,同時我的查詢沒有按照我的sql語句完成 – DevelopmentIsMyPassion 2013-02-11 17:41:41

回答

1

終於託管了。這是我的查詢。

[WebInvoke(Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "ProductLanguageList")] 
    public List<LookUpProductLanguage> GetProductLanguageList(GetParameters param) 
    { 
     var passedProductId = int.Parse(param.ProductId); 
     var query = from languageEntity in _languageEntityRepository.AsQueryable() 
        join subQueryResult in 
         (from productLanguageEntity in _productLanguageEntityRepository.AsQueryable() 
         join productEntity in _productRepository.AsQueryable() on 
          productLanguageEntity.LanguageProductId equals productEntity.Id into 
          joinedProductLanguage 
         from productLanguageJoin in joinedProductLanguage.DefaultIfEmpty() 
         where productLanguageJoin.Id.Equals(passedProductId) 
         select new {productLanguageEntity.LanguageId} 
         ) on languageEntity.Id equals subQueryResult.LanguageId into a 
        from b in a.DefaultIfEmpty() 
        where languageEntity.Closed == 0 

        select new LookUpProductLanguage {LanguageId = languageEntity.Id, LanguageName = languageEntity.Name,LanguageCode = languageEntity.Language,LanguageProductId = b.LanguageId }; 

     return query.ToList(); 

    } 

非常感謝的傢伙們努力試圖回答我。